package hai.test.videoPlayer;
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.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class VideoPlayerActivity extends Activity {
private MediaPlayer mediaPlayer;
private EditText filenameText;
private Button pause;
private String filename;
private int position;
private SurfaceView surfaceView;
private static final String TAG="MP3PlayerActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ButtonClick buttonClick=new ButtonClick();
mediaPlayer = new MediaPlayer();
surfaceView = (SurfaceView)this.findViewById(R.id.surfaceView);
surfaceView.getHolder().setFixedSize(176, 144); //设置分辨率
/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Button play =(Button)findViewById(R.id.play);
pause =(Button)findViewById(R.id.pause);
Button resume =(Button)findViewById(R.id.resume);
Button stop =(Button)findViewById(R.id.stop);
filenameText =(EditText)findViewById(R.id.filename);
play.setOnClickListener(buttonClick);
pause.setOnClickListener(buttonClick);
resume.setOnClickListener(buttonClick);
stop.setOnClickListener(buttonClick);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
position=savedInstanceState.getInt("position");
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
if(mediaPlayer.isPlaying()){
outState.putInt("position",mediaPlayer.getCurrentPosition());
}
super.onSaveInstanceState(outState);
}
@Override
protected void onPause() {
if(mediaPlayer.isPlaying()){
position=mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
}
super.onPause();
}
@Override
protected void onResume() {
if(position>0)
{ mediaPlayer.seekTo(position);
mediaPlayer.start();
}
super.onResume();
}
private class ButtonClick implements View.OnClickListener{
@Override
public void onClick(View v) {
try {
filename=filenameText.getText().toString();
switch (v.getId()) {
case R.id.play:
paly();
break;
case R.id.pause:
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
pause.setText(R.string.contiue);
}else {
mediaPlayer.start();
pause.setText(R.string.pause);
}
break;
case R.id.resume:
if(!mediaPlayer.isPlaying()){
paly();
}else {
mediaPlayer.seekTo(0);
}
break;
case R.id.stop:
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
break;
}
} catch (Exception e) {
Log.e(TAG,e.toString());
}
}
private void paly() throws IOException {
surfaceView.getHolder().setFixedSize(176, 144); //设置分辨率
/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
File file=new File(Environment.getExternalStorageDirectory(),filename);
mediaPlayer.reset();//重置为初始状态
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
/* 设置Video影片以SurfaceHolder播放 */
mediaPlayer.setDisplay(surfaceView.getHolder());
mediaPlayer.setDataSource(file.getAbsolutePath());
mediaPlayer.prepare();//缓冲
mediaPlayer.start();//播放
}
}
}