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"
>
<SurfaceView android:id="@+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="360px"
/>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
>
<Button android:id="@+id/start"
android:text="开始"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/pause"
android:text="暂停"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/stop"
android:text="停止"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
MainActivity.java
import java.io.IOException;
import javax.security.auth.callback.Callback;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity implements OnClickListener{
private Button buttonStart;
private Button buttonPause;
private Button buttonStop;
SurfaceView surfaceView;
MediaPlayer mediaPlayer;
int position;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.buttonStart = (Button) this.findViewById(R.id.start);
this.buttonPause = (Button) this.findViewById(R.id.pause);
this.buttonStop = (Button) this.findViewById(R.id.stop);
this.buttonStart.setOnClickListener(this);
this.buttonPause.setOnClickListener(this);
this.buttonStop.setOnClickListener(this);
this.mediaPlayer = new MediaPlayer();
this.surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
//设置SurfaceView自己不管理的缓冲区
this.surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
if(position>0){
try {
play();
//并直接从指定位置开始播放
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaPlayer.seekTo(position);
position=0;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.start:
try {
play();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.pause:
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
}else{
mediaPlayer.start();
}
break;
case R.id.stop:
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
break;
default:
break;
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
if(mediaPlayer.isPlaying()){
this.position = this.mediaPlayer.getCurrentPosition();
this.mediaPlayer.stop();
}
super.onPause();
}
void play() throws IllegalArgumentException, IllegalStateException, IOException{
this.mediaPlayer.reset();
this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
this.mediaPlayer.setDataSource("/mnt/sdcard/DCIM/Camera/123.3gp");
//把视频画面输出到SurfaceView
this.mediaPlayer.setDisplay(this.surfaceView.getHolder());
this.mediaPlayer.prepare();
this.mediaPlayer.start();
}
}