只有一个主Activity,处理一切事情
//程序运行效果
//看看代码
package sn.len.video; import java.io.File; import java.io.IOException; import Android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Environment; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.ImageButton; public class VideoPlayerActivity extends Activity implements OnClickListener { private EditText editText; private String fileName; private MediaPlayer mediaPlayer; private SurfaceView surfaceView; private int position; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mediaPlayer=new MediaPlayer(); surfaceView=(SurfaceView)findViewById(R.id.surfaceview); //设置surfaceView的一些初始化参数 //得到要操作SurfaceView的对象 SurfaceHolder surfaceHolder=surfaceView.getHolder(); //设置屏幕像索 surfaceHolder.setFixedSize(200, 200); //不用屏幕缓冲 surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //处理当电话来电时,而addCallback里面是一个接口,所以弄个类来实现它才行 surfaceHolder.addCallback(new MyCallback()); editText=(EditText)findViewById(R.id.fileName); fileName=editText.getText().toString(); ImageButton start=(ImageButton)findViewById(R.id.play); ImageButton pause=(ImageButton)findViewById(R.id.pause); ImageButton replay=(ImageButton)findViewById(R.id.reset); ImageButton stop=(ImageButton)findViewById(R.id.stop); start.setOnClickListener(this); pause.setOnClickListener(this); replay.setOnClickListener(this); stop.setOnClickListener(this); } private class MyCallback implements SurfaceHolder.Callback { //当第二次surface创建的时候,已经拿到了上次的断点就可以接着放了。 @Override public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) { if(position>0 && fileName!=null) { try { play(); mediaPlayer.seekTo(position); position=0; } catch (IOException e) { e.printStackTrace(); } } } @Override public void surfaceCreated(SurfaceHolder holder) { } //当电话来的时候,也就是surface要消毁的时候,记录视频的位置 @Override public void surfaceDestroyed(SurfaceHolder holder) { position=mediaPlayer.getCurrentPosition(); mediaPlayer.stop(); } } @Override public void onClick(View v) { //可以判断下SD卡是否存在 //Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) try { switch (v.getId()) { case R.id.play: { play(); }break; case R.id.pause: { if(mediaPlayer.isPlaying()) { mediaPlayer.pause(); } else { mediaPlayer.start(); } }break; case R.id.reset: { if(mediaPlayer.isPlaying()) { mediaPlayer.seekTo(0); } else { play(); } }break; case R.id.stop: { if(mediaPlayer.isPlaying()) { mediaPlayer.stop(); } }break; default:break; } } catch (IOException e) { e.printStackTrace(); } } private void play() throws IOException { File videoFile=new File(Environment.getExternalStorageDirectory(),fileName); mediaPlayer.reset(); //设置影片类型 mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); //让surfaceView来播放 mediaPlayer.setDisplay(surfaceView.getHolder()); //设置路径 mediaPlayer.setDataSource(videoFile.getAbsolutePath()); mediaPlayer.prepare();//缓冲 mediaPlayer.start();//播放 } }