Android--VideoPlay--视频播放器
代码跟音乐播放器有大部分是相同的,各位可以将mp4格式的
视频文件放到sdcard卡中,在输入框中输入文件名即可,在
模拟器上运行请使用android2.0版本,据说其他版本的模拟器
不支持视频播放,如有错误之处请大家指出
java代码如下:
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代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFFFFF" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/filename" />
<EditText
android:id="@+id/filename"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/file"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/playbutton"
android:onClick="mediaplay"
android:text="@string/playbutton"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pausebutton"
android:onClick="mediaplay"
android:text="@string/pausebutton"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/resetbutton"
android:onClick="mediaplay"
android:text="@string/resetbutton"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stopbutton"
android:onClick="mediaplay"
android:text="@string/stopbutton"
/>
</LinearLayout>
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="240dp"
android:id="@+id/surfaceview"
/>
</LinearLayout>
string.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="filenoexsit">视频文件不存在</string>
<string name="app_name">视频播放器</string>
<string name="filename">视频文件名称</string>
<string name="playbutton">播放</string>
<string name="pausebutton">暂停</string>
<string name="resetbutton">重播</string>
<string name="stopbutton">停止</string>
<string name="file">OPPO.mp4</string>
</resources>
资源下载地址:http://download.csdn.net/detail/zlqqhs/4870784
资源里面有一个mp4文件,可直接放到 sdcard 卡中使用