mediaPlayer.release();//释放资源
package cn.itcast.video; import java.io.IOException; import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.EditText; import android.widget.ImageButton; public class VideoActivity extends Activity { private static final String TAG = "VideoActivity"; private EditText filenameText; private SurfaceView surfaceView; private MediaPlayer mediaPlayer; private String filename;//当前播放文件的名称 private int position;//记录播放位置 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.mediaPlayer = new MediaPlayer(); this.filenameText = (EditText) this.findViewById(R.id.filename); this.surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView); ImageButton playButton = (ImageButton) this.findViewById(R.id.play); ImageButton pauseButton = (ImageButton) this.findViewById(R.id.pause); ImageButton resetButton = (ImageButton) this.findViewById(R.id.reset); ImageButton stopButton = (ImageButton) this.findViewById(R.id.stop); ButtonClickListener listener = new ButtonClickListener(); playButton.setOnClickListener(listener); pauseButton.setOnClickListener(listener); resetButton.setOnClickListener(listener); stopButton.setOnClickListener(listener); /*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/ this.surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); this.surfaceView.getHolder().setFixedSize(176, 144);//设置分辨率 this.surfaceView.getHolder().setKeepScreenOn(true); this.surfaceView.getHolder().addCallback(new SurfaceListener()); } private class ButtonClickListener implements View.OnClickListener{ @Override public void onClick(View v) { try { switch (v.getId()) { case R.id.play://来自播放按钮 filename = filenameText.getText().toString(); play(); break; case R.id.pause://来自暂停按钮 if(mediaPlayer.isPlaying()){ mediaPlayer.pause(); }else{ mediaPlayer.start(); } break; case R.id.reset://来自重新播放按钮 if(!mediaPlayer.isPlaying()) play(); mediaPlayer.seekTo(0); break; case R.id.stop://来自停止按钮 if(mediaPlayer.isPlaying()) mediaPlayer.stop(); break; } } catch (Exception e) { Log.e(TAG, e.toString()); } } } /** * 播放视频 */ private void play() throws IOException { mediaPlayer.reset(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource("/mnt/sdcard/"+ filename);//设置需要播放的视频 mediaPlayer.setDisplay(surfaceView.getHolder());//把视频画面输出到SurfaceView mediaPlayer.prepare(); mediaPlayer.start(); } private class SurfaceListener implements SurfaceHolder.Callback{ @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) {//方法在onResume()后被调用 Log.i(TAG, "surfaceCreated()"); if(position>0 && filename!=null){ try { play(); mediaPlayer.seekTo(position); position = 0; } catch (Exception e) { Log.e(TAG, e.toString()); } } } @Override public void surfaceDestroyed(SurfaceHolder holder) { Log.i(TAG, "surfaceDestroyed()"); } } @Override protected void onPause() {//当其他Activity被打开,停止播放 if(mediaPlayer.isPlaying()){ position = mediaPlayer.getCurrentPosition();//得到播放位置 mediaPlayer.stop(); } super.onPause(); } @Override protected void onDestroy() { if(mediaPlayer.isPlaying()) mediaPlayer.stop(); mediaPlayer.release(); super.onDestroy(); } } <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="#FFFFFF" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/filename" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="oppo.mp4" android:id="@+id/filename" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/play" android:id="@+id/play" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pause" android:id="@+id/pause" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/reset" android:id="@+id/reset" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/stop" android:id="@+id/stop" /> </LinearLayout> <SurfaceView android:layout_width="fill_parent" android:layout_height="240dip" android:id="@+id/surfaceView" /> </LinearLayout>示例代码:
DemoActivity.java:
package cn.itcast.videoplayer; import java.io.File; import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnErrorListener; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class DemoActivity extends Activity implements OnClickListener { private EditText et_path; private Button bt_start, bt_pause, bt_restart, bt_stop; private SurfaceView sv; private SurfaceHolder holder; MediaPlayer mediaPlayer; boolean ispause = false; int position = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et_path = (EditText) this.findViewById(R.id.et_path); bt_start = (Button) this.findViewById(R.id.bt_start); bt_pause = (Button) this.findViewById(R.id.bt_pause); bt_restart = (Button) this.findViewById(R.id.bt_restart); bt_stop = (Button) this.findViewById(R.id.bt_stop); bt_start.setOnClickListener(this); bt_pause.setOnClickListener(this); bt_restart.setOnClickListener(this); bt_stop.setOnClickListener(this); sv = (SurfaceView) this.findViewById(R.id.sv); // 视频播放的填充器 holder = sv.getHolder(); /* 下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前 */ holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // holder.setFixedSize(width, height); holder.addCallback(new MyHolderCallback()); } @Override protected void onStart() { // TODO Auto-generated method stub // 后台继续播放 回到 前台 仍然能看到画面 super.onStart(); } private class MyHolderCallback implements SurfaceHolder.Callback { public void surfaceCreated(SurfaceHolder holder) { System.out.println("holder 被创建 "); // 当视频播放最小化之后 下一次 必须在holder被创建后 再去播放视频 if (position > 0) { String path = et_path.getText().toString().trim(); try { // 重新设置显示的holder DemoActivity.this.holder = holder; play(path, position); } catch (Exception e) { Toast.makeText(getApplicationContext(), "播放出现异常", 0).show(); e.printStackTrace(); } } } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } public void surfaceDestroyed(SurfaceHolder holder) { System.out.println("holder 被销毁 "); // 记录最小化之后播放器播放的位置 if (mediaPlayer != null && mediaPlayer.isPlaying()) { position = mediaPlayer.getCurrentPosition(); mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null; } } } public void onClick(View v) { String path = et_path.getText().toString().trim(); try { switch (v.getId()) { case R.id.bt_start: play(path, 0); break; case R.id.bt_restart: if (mediaPlayer != null && mediaPlayer.isPlaying()) { mediaPlayer.seekTo(0); } else { play(path, 0); } break; case R.id.bt_stop: if (mediaPlayer != null && mediaPlayer.isPlaying()) { mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null; bt_start.setEnabled(true); } break; case R.id.bt_pause: pause(); break; } } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "视频播放失败", 0).show(); } } private void pause() { if (mediaPlayer != null && mediaPlayer.isPlaying()) { mediaPlayer.pause(); bt_pause.setText("☺"); ispause = true; return; } if (mediaPlayer != null && ispause) { mediaPlayer.start(); bt_pause.setText("‖"); ispause = false; } } /** * 根据路径 进行播放 * * @param path */ private void play(String path, int position) throws Exception { if ("".equals(path)) { Toast.makeText(this, "路径不能为空", 0).show(); return; } File file = new File(path); if (file.exists()) { mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource(path); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDisplay(holder); // 播放视频之前必须要初始化 视频的播放器 mediaPlayer.prepare(); mediaPlayer.start(); mediaPlayer.seekTo(position); bt_start.setEnabled(false); mediaPlayer.setOnCompletionListener(new OnCompletionListener() { public void onCompletion(MediaPlayer mp) { mediaPlayer.release(); mediaPlayer = null; bt_start.setEnabled(true); } }); // 当播放出现错误的时候的回调方法 mediaPlayer.setOnErrorListener(new OnErrorListener() { public boolean onError(MediaPlayer mp, int what, int extra) { mp.release(); mediaPlayer = null; bt_start.setEnabled(true); return false; } }); } else { Toast.makeText(this, "文件不存在", 0).show(); return; } } }main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请输入视频的路径" /> <EditText android:id="@+id/et_path" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="mnt/sdcard/oop.mp4" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/bt_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=">" /> <Button android:id="@+id/bt_pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="‖" /> <Button android:id="@+id/bt_restart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="◎" /> <Button android:id="@+id/bt_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="■" /> </LinearLayout> <SurfaceView android:id="@+id/sv" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
MediaPlayer生命周期: