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.Su易做图ceHolder;
import android.view.Su易做图ceHolder.Callback;
import android.view.Su易做图ceView;
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 Su易做图ceView su易做图ceView;
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);
su易做图ceView = (Su易做图ceView)findViewById(R.id.su易做图ceview);
//把输送给su易做图ceview的视频画面,直接显示到屏幕上,不要维持它自身的缓冲区
su易做图ceView.getHolder().setType(Su易做图ceHolder.SURFACE_TYPE_PUSH_BUFFERS);
su易做图ceView.getHolder().setFixedSize(176, 144);//设置视频播放窗口的大小
su易做图ceView.getHolder().setKeepScreenOn(true);//保持屏幕高亮
su易做图ceView.getHolder().addCallback(new Su易做图ceCallback());
mediaPlayer = new MediaPlayer();//实例化一个播放器对象
}
private final class Su易做图ceCallback implements Callback{
public void su易做图ceChanged(Su易做图ceHolder holder, int format, int width,
int height) {
}
/*
* 当su易做图ceView被创建时
*/
public void su易做图ceCreated(Su易做图ceHolder holder) {
if(position > 0 && path != null){
play(position);
position = 0;
}
}
/*
* 当su易做图ceView被销毁时
*/
public void su易做图ceDestroyed(Su易做图ceHolder 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(su易做图ceView.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"
/>
&nbs
补充:移动开发 , Android ,