Android 简单视频播放器(破烂版,后续更新)
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文件名称" />
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="oppo.mp4"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageButton
android:id="@+id/btn_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play"
/>
<ImageButton
android:id="@+id/btn_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pause"
/>
<ImageButton
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stop"
/>
<ImageButton
android:id="@+id/btn_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/reset"
/>
</LinearLayout>
<SurfaceView
android:id="@+id/sv_video"
android:layout_width="match_parent"
android:layout_height="240dp"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private EditText edit;
private ImageButton btn_play;
private ImageButton btn_pause;
private ImageButton btn_stop;
private ImageButton btn_reset;
private SurfaceView sv;
private MediaPlayer mediaPlayer;
private String filename;
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.edit);
btn_play = (ImageButton) findViewById(R.id.btn_play);
btn_pause = (ImageButton) findViewById(R.id.btn_pause);
btn_stop = (ImageButton) findViewById(R.id.btn_stop);
btn_reset = (ImageButton) findViewById(R.id.btn_reset);
sv = (SurfaceView) findViewById(R.id.sv_video);
sv.getHolder().setFixedSize(176, 144);
sv.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
sv.getHolder().addCallback(new SurfaceCallback());
BtnListener btnListener = new BtnListener();
mediaPlayer = new MediaPlayer();
btn_play.setOnClickListener(btnListener);
btn_pause.setOnClickListener(btnListener);
btn_stop.setOnClickListener(btnListener);
btn_reset.setOnClickListener(btnListener);
}
private final class SurfaceCallback implements SurfaceHolder.Callback {
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if(position > 0 && filename != null) {
try {
play();
mediaPlayer.seekTo(position);
position = 0;
} catch (Exception e) {
Log.i("VedioPlayer", e.toString());
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(mediaPlayer.isPlaying()) {
position = mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mediaPlayer.release();
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
this.filename = savedInstanceState.getString("filename");
this.position = savedInstanceState.getInt("position");
Log.i("MediaPlayer", "onRestoreInstanceState");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("filename", filename);
outState.putInt("position", position);
Log.i("VedioPlayer", "onSaveInstanceState");
}
private final class BtnListener implements OnClickListener {
@Override
public void onClick(View v) {
if(!Environment.getExternalStorageState()
.equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(MainActivity.this, "SD卡还未准备好", 1000).show();
return;
}
filename = edit.getText().toString();
ImageButton btn = (ImageButton) v;
try {
switch (btn.getId()) {
case R.id.btn_play:
play();
break;
case R.id.btn_pause:
if(mediaPlayer.isPlaying()) {
mediaPlayer.pause();
btn.setImageResource(R.drawable.play);
} else {
mediaPlayer.start();
btn.setImageResource(R.drawable.pause);
}
break;
case R.id.btn_stop:
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
break;
case R.id.btn_reset:
if(mediaPlayer.isPlaying()) {
mediaPlayer.seekTo(0);
} else {
play();
}
break;
default:
break;
}
} catch (Exception e) {
Log.i("VedioPlayer", e.toString());
}
}
}
private void play() throws IOException {
File audioFile = new File(
Environment.getExternalStorageDirectory(), filename);
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(sv.getHolder());
mediaPlayer.setDataSource(audioFile.getAbsolutePath());
mediaPlayer.prepare();
mediaPlayer.start();
}
}