本文主要是介绍Android自带mediaplayer封装,实现播放,暂停,拖动等功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
封装代码工具类:
public class AudioPlayerUtil {private MediaPlayer mPlayer;private boolean isInitialized = false;private Thread initThread;//实现进度条private Timer mTimer;private Handler mHandler;private String url;public AudioPlayerUtil(String url) {this.url = url;mTimer = new Timer();mHandler = new Handler();initThread = new Thread(() -> init(url));initThread.start();mTimer.schedule(new TimerTask() {@Overridepublic void run() {if (isPlaying()) {int pos = mPlayer.getCurrentPosition();int duration = mPlayer.getDuration();mHandler.post(() -> {if (mOnSeekListener != null) {mOnSeekListener.OnSeek((int) (pos * 1.f / duration * 100));}});}}}, 0, 1000);}private void init(String url) {mPlayer = new MediaPlayer();try {mPlayer.setDataSource(url);} catch (IOException e) {e.printStackTrace();}mPlayer.prepareAsync();isInitialized = true;mPlayer.setOnErrorListener((mp, what, extra) -> {//处理错误return false;});//音频播放完成mPlayer.setOnCompletionListener(mp -> {//start();//播放完成再播放--实现单曲循环if (mOnSeekListener != null) {mOnSeekListener.onComplete();}//init(url);});//音频加载完成可以播放mPlayer.setOnPreparedListener(mp -> {if (mOnSeekListener != null) {mOnSeekListener.loadComplete();}});}/*** 播放*/public void start() {if (mPlayer == null) return;if (!isInitialized && mPlayer.isPlaying()) {return;}mPlayer.start();}/*** 是否正在播放*/public boolean isPlaying() {if (!isInitialized) {return false;}return mPlayer.isPlaying();}/*** 销毁播放器*/public void onDestroyed() {if (mPlayer != null) {mPlayer.stop();mPlayer.release();//释放资源mPlayer = null;}isInitialized = false;}/*** 停止播放器*/public void stop() {if (mPlayer != null && mPlayer.isPlaying()) {mPlayer.stop();}}/*** 暂停播放器*/public void pause() {if (mPlayer != null && mPlayer.isPlaying()) {mPlayer.pause();}}/*** 跳转到** @param pre_100 0~100*/public void seekTo(int pre_100) {pause();mPlayer.seekTo((int) (pre_100 / 100.f * mPlayer.getDuration()));start();}//------------设置进度监听-----------public interface OnSeekListener {void OnSeek(int per_100);void onComplete();void loadComplete();}private OnSeekListener mOnSeekListener;public void setOnSeekListener(OnSeekListener onSeekListener) {mOnSeekListener = onSeekListener;}}
使用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><SeekBarandroid:id="@+id/seek_bar"android:layout_width="0dp"android:layout_height="5dp"android:layout_marginTop="@dimen/dp_10"android:layout_marginBottom="@dimen/dp_10"android:layout_weight="1" /><TextViewandroid:id="@+id/tv_index"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0" /><TextViewandroid:id="@+id/tv_total"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="/100" /></LinearLayout><TextViewandroid:id="@+id/tv_start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="@dimen/dp_10"android:text="start" /><TextViewandroid:id="@+id/tv_stop"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="@dimen/dp_10"android:text="stop" /></LinearLayout>
var url = "xxx.mp3"override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_music)val musicPlayer = AudioPlayerUtil(url)tv_start.setOnClickListener {musicPlayer.start()}tv_stop.setOnClickListener {if (musicPlayer.isPlaying) {musicPlayer.pause()}}seek_bar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {}override fun onStartTrackingTouch(p0: SeekBar?) {}override fun onStopTrackingTouch(p0: SeekBar?) {p0?.progress?.let { musicPlayer.seekTo(it) }}})musicPlayer.setOnSeekListener(object : AudioPlayerUtil.OnSeekListener {override fun OnSeek(per_100: Int) {seek_bar.progress = per_100tv_index.text = per_100.toString()}override fun onComplete() {seek_bar.progress = 0tv_index.text = "0"}override fun loadComplete() {}})}
这篇关于Android自带mediaplayer封装,实现播放,暂停,拖动等功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!