一、概述:

因为播放视频需要很大的内存,所以必须使用surfaceview ,
surfaceview 里实现了双缓冲的功能,

二、全部代码:

/**
* @描述 使用surfaceview 创建视频
* @项目名称 App_Basic
* @包名 com.example.basic.media
* @类名 MediaPlayerActivity
* @author chenlin
* @date 2013-3-25 上午7:36:32
* @version 1.0
*/

public class MediaPlayerActivity extends BaseActivity {
private MediaPlayer mPlayer;
private Button mBtnPlay;
private Button mBtnPause;
private SurfaceView mSurfaceView;
private int mCurrentPosition;
private SeekBar seekBar;
private boolean isPlaying;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSurfaceView = new SurfaceView(this);
seekBar = new SeekBar(this);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
if (mPlayer != null && isPlaying) {
mPlayer.seekTo(progress);
}

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
}
});
mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mSurfaceView.getHolder().addCallback(new Callback() {


@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mPlayer!= null && mPlayer.isPlaying()) {
mCurrentPosition = mPlayer.getCurrentPosition();
stop();
}

}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (mCurrentPosition > 0) {
play(mCurrentPosition);
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
});
}

public void play(final int currentPosition){
try {
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
Uri myUri = Uri.fromFile(new File("sdcard/mp3/1.mp3"));
mPlayer.setDataSource(getApplicationContext(), myUri);
mPlayer.setLooping(true);
mPlayer.setDisplay(mSurfaceView.getHolder());
mPlayer.prepareAsync();
//mPlayer.prepare();
mBtnPlay.setEnabled(false);
mPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mBtnPlay.setEnabled(true);
mPlayer.release();
}
});

mPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
mBtnPlay.setEnabled(true);
mPlayer.release();
isPlaying = false;
return false;
}
});

mPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mPlayer.start();
mPlayer.seekTo(currentPosition);
seekBar.setMax(mPlayer.getDuration());
new Thread(new Runnable() {
@Override
public void run() {
isPlaying = true;
while(isPlaying){
seekBar.setProgress(mPlayer.getCurrentPosition());
SystemClock.sleep(500);
}
}
});
}
});

} catch (Exception e) {
e.printStackTrace();
}
}

public void pause(){
if ("接续".equals(mBtnPause.getText().toString())) {
mBtnPause.setText("暂停");
mPlayer.start();
return;
}
if (mPlayer != null && mPlayer.isPlaying()) {
mPlayer.pause();
mBtnPause.setText("继续");
return;
}
}

public void reset(){
if (mPlayer != null && mPlayer.isPlaying()) {
mPlayer.seekTo(0);
return;
}
play(0);
}

public void stop(){
if (mPlayer != null && mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
mBtnPlay.setEnabled(true);
isPlaying = false;
}
}
}