初始化Mediaplayer
方法一
//1、实例化mediaPlay MediaPlayer mediaPlayer=new MediaPlayer(); AssetFileDescriptor assetFileDescriptor=assetManager.openFd("ynzz.mp3"); //2、设置播放的数据源 以下是Assert的方式 还可以使用URL等其他方式 mediaPlayer.setDataSource( assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength()); //3、准备播放 mediaPlayer.prepare();
方法二
//raw方式直接设置了数据源 并且PrePare()已经被调用 MediaPlayer进入PrePared状态 mediaPlayer=MediaPlayer.create(this, R.raw.ynzz);
执行操作
//播放 mediaPlayer.start(); //暂停 mediaPlayer.pause(); //停止 mediaPlayer.stop(); //释放资源 mediaPlayer.release() //添加缓冲监听 mediaPlayer.setOnBufferingUpdateListener(bufferingUpdateListener);
一个播放音频的完整例子
package com.linxcool.activity; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaPlayer.OnSeekCompleteListener; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.SeekBar; public class MediasActivity extends Activity { //按钮:播放 暂停 停止 private Button btnPlay,btnPause,btnStop; //进度条 private SeekBar seekBar; //改变进度条进度的句柄handler 标志isNeedChangeProgress 进程pThread private Handler handler; private boolean isNeedChangeProgress; private Thread pThread; //播放状态:播放 暂停 停止 private enum Status{PALYING,PAUSE,STOP} //当前播放状态 public Status currentStatus; //媒体播放器 private MediaPlayer mediaPlayer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); currentStatus=Status.STOP; isNeedChangeProgress=true; initBtn(); initSeekBar(); initHandle(); } private void initBtn(){ btnPlay=(Button)findViewById(R.id.btnPlay); btnPause=(Button)findViewById(R.id.btnPause); btnStop=(Button)findViewById(R.id.btnStop); OnClickListener listener=new OnClickListener() { @Override public void onClick(View v) { if(v==btnPlay){ if(currentStatus==Status.STOP)initMedia(); mediaPlayer.start(); isNeedChangeProgress=true; if(pThread==null){ initThread(); pThread.start(); } currentStatus=Status.PALYING; } else if(v==btnPause && currentStatus==Status.PALYING){ currentStatus=Status.PAUSE; mediaPlayer.pause(); } else if(v==btnStop && currentStatus!=Status.STOP){ mediaPlayer.stop(); currentStatus=Status.STOP; isNeedChangeProgress=false; pThread=null; mediaPlayer.release(); } } }; btnPlay.setOnClickListener(listener); btnPause.setOnClickListener(listener); btnStop.setOnClickListener(listener); } private void initMedia(){ mediaPlayer=new MediaPlayer(); mediaPlayer=MediaPlayer.create(this, R.raw.ynzz); mediaPlayer.setOnSeekCompleteListener(new OnSeekCompleteListener() { @Override public void onSeekComplete(MediaPlayer mp) { isNeedChangeProgress=false; pThread=null; } }); } private void initSeekBar(){ initMedia(); seekBar=(SeekBar)findViewById(R.id.seekBar); seekBar.setMax(mediaPlayer.getDuration()); } private void initHandle(){ handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); seekBar.setProgress(msg.arg1); } }; } private void initThread(){ pThread=new Thread(){ @Override public void run() { while(isNeedChangeProgress){ try { Message msg=Message.obtain(); msg.arg1=mediaPlayer.getCurrentPosition(); System.out.println(msg.arg1); handler.sendMessage(msg); Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } }; } }