效果图
知识要点
surfaceView.getHolder().setFixedSize(176, 144);//设置分辨率
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//设置surfaceview不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前
surfaceView.getHolder().addCallback(new SurceCallBack());//对surface对象的状态进行监听
mediaPlayer.reset();//重置为初始状态
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置音乐流的类型
mediaPlayer.setDisplay(surfaceView.getHolder());//设置video影片以surfaceviewholder播放
mediaPlayer.setDataSource(file.getAbsolutePath());//设置路径
mediaPlayer.prepare();//缓冲
mediaPlayer.start();//播放
相关代码


<resources>
<string name="hello">Hello World, VodeoPlayActivity!</string>
<string name="app_name">视频播放器</string>
<string name="filename">视频文件</string>
<string name="sdcarderror">sd卡不存在</string>
</resources>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
>
<TextView
android:layout_width="wrap_content"
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="fill_parent"
android:src="@drawable/play"
android:id="@+id/play"
/>
<ImageButton android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/pause"
android:id="@+id/pause"
/>
<ImageButton android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/stop"
android:id="@+id/stop"
/>
<ImageButton android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/reset"
android:id="@+id/reset"
/>
</LinearLayout>
<SurfaceView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/surfaceview"
/>
</LinearLayout>


import java.io.File;
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.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class VodeoPlayActivity extends Activity {
/** Called when the activity is first created. */
private EditText filenamEditText;
private MediaPlayer mediaPlayer;
private String filename;
private SurfaceView surfaceView;
private final static String TAG="VodeoPlayActivity";
private int prosition=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
filenamEditText=(EditText) this.findViewById(R.id.filename);
surfaceView=(SurfaceView)this.findViewById(R.id.surfaceview);
surfaceView.getHolder().setFixedSize(176, 144);//设置分辨率
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//设置surfaceview不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前
surfaceView.getHolder().addCallback(new SurceCallBack());//对surface对象的状态进行监听
mediaPlayer=new MediaPlayer();
ButtonOnClikListiner buttonOnClikListinero=new ButtonOnClikListiner();
ImageButton start=(ImageButton) this.findViewById(R.id.play);
ImageButton pause=(ImageButton) this.findViewById(R.id.pause);
ImageButton stop=(ImageButton) this.findViewById(R.id.stop);
ImageButton replay=(ImageButton) this.findViewById(R.id.reset);
start.setOnClickListener(buttonOnClikListinero);
pause.setOnClickListener(buttonOnClikListinero);
stop.setOnClickListener(buttonOnClikListinero);
replay.setOnClickListener(buttonOnClikListinero);
}
/* @Override
protected void onPause() {
if(mediaPlayer.isPlaying()){
prosition=mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
}
super.onPause();
}
@Override
protected void onResume() {
if(prosition>0&&filename!=null){
play();
mediaPlayer.seekTo(prosition);
prosition=0;
}
super.onResume();
}*/
private final class ButtonOnClikListiner implements View.OnClickListener{
@Override
public void onClick(View v) {
if(Environment.getExternalStorageState()==Environment.MEDIA_UNMOUNTED){
Toast.makeText(VodeoPlayActivity.this, "sd卡不存在", Toast.LENGTH_SHORT).show();
return;
}
filename=filenamEditText.getText().toString();
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;
}
}
}
private void play() {
try {
File file=new File(Environment.getExternalStorageDirectory(),filename);
mediaPlayer.reset();//重置为初始状态
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置音乐流的类型
mediaPlayer.setDisplay(surfaceView.getHolder());//设置video影片以surfaceviewholder播放
mediaPlayer.setDataSource(file.getAbsolutePath());//设置路径
mediaPlayer.prepare();//缓冲
mediaPlayer.start();//播放
} catch (Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
}
private final class SurceCallBack implements SurfaceHolder.Callback{
/**
* 画面修改
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
/**
* 画面创建
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
if(prosition>0&&filename!=null){
play();
mediaPlayer.seekTo(prosition);
prosition=0;
}
}
/**
* 画面销毁
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(mediaPlayer.isPlaying()){
prosition=mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
}
}
}
}