代码跟音乐播放器有大部分是相同的,各位可以将mp4格式的
视频文件放到sdcard卡中,在输入框中输入文件名即可,在
模拟器上运行请使用android2.0版本,据说其他版本的模拟器
不支持视频播放,如有错误之处请大家指出
java代码如下:
[html] view plaincopy
package cn.mrzhu;
import java.io.File;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class VideoPlayDemoActivity extends Activity {
private EditText nameText;
private String path;//文件路径
private MediaPlayer mediaPlayer;//media对象
private SurfaceView surfaceView;
private boolean pause;//暂停标记
private int position;//播放的进度
/* Called when the activity is first created. /
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nameText = (EditText)findViewById(R.id.filename);
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
//把输送给surfaceview的视频画面,直接显示到屏幕上,不要维持它自身的缓冲区
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().setFixedSize(176, 144);//设置视频播放窗口的大小
surfaceView.getHolder().setKeepScreenOn(true);//保持屏幕高亮
surfaceView.getHolder().addCallback(new SurfaceCallback());
mediaPlayer = new MediaPlayer();//实例化一个播放器对象
}
private final class SurfaceCallback implements Callback{
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
/*
* 当surfaceView被创建时
*/
public void surfaceCreated(SurfaceHolder holder) {
if(position > 0 && path != null){
play(position);
position = 0;
}
}
/*
* 当surfaceView被销毁时
*/
public void surfaceDestroyed(SurfaceHolder holder) {
mediaPlayer.stop();
}
}
/*
* 按钮事件,在main.xml中,每个按钮的click_on = mediaplay
*/
public void mediaplay(View v){
switch(v.getId()){
case R.id.playbutton://播放按钮
String filename = nameText.getText().toString();//获取文件名
File file = new File(Environment.getExternalStorageDirectory(), filename);
if(file.exists()){
path = file.getAbsolutePath();
play(0);
}else{
//若路径不存在,弹出Toast提示
Toast.makeText(getApplicationContext(), R.string.filenoexsit, Toast.LENGTH_SHORT).show();
}
break;
case R.id.pausebutton://暂停按钮
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
pause = true;
}else{
mediaPlayer.start();
pause = false;
}
break;
case R.id.resetbutton://重播按钮
if(mediaPlayer.isPlaying()){
mediaPlayer.seekTo(0);
}else{
if(path != null){
play(0);//将播放进度置为0,恢复初始状态
}
}
break;
case R.id.stopbutton://停止按钮
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
break;
}
}
private void play(int position) {
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(path);
mediaPlayer.setDisplay(surfaceView.getHolder());
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(new PrepareListener(position));
} catch (Exception e) {
}
}
private final class PrepareListener implements OnPreparedListener{
private int position;
public PrepareListener(int position) {
super();
this.position = position;
}
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();//播放视频
if(position > 0) mediaPlayer.seekTo(position);
}
}
@Override
protected void onDestroy() {
mediaPlayer.release();
mediaPlayer = null;
super.onDestroy();
}
}
main.xml代码如下:
[html] view plaincopy