Android VideoView
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView android:id="@+id/videoview" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<SurfaceView android:focusable="true" android:id="@+id/gameSurfaceView"
android:layout_width="fill_parent" android:clickable="true"
android:focusableInTouchMode="true" android:layout_height="fill_parent"></SurfaceView>
</LinearLayout>
类文件
package com.cgw;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;
public class VideoViewDemo extends Activity implements OnErrorListener,OnCompletionListener{
private VideoView mVideoView;
private Uri mUri;
private int mPositionWhenPaused = -1;
private MediaController mMediaController;//视频的控制条 如果不想要就不用设置。
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 全屏模式
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
mVideoView = (VideoView)findViewById(R.id.videoview);
mVideoView.setOnCompletionListener(this);
mVideoView.setOnErrorListener(this);
//文件路径
mUri = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.movie);//mp4格式的文件也一样可以播放。
// System.out.println(mUri.getPath());
// mMediaController = new MediaController(this);
//设置MediaController
// mVideoView.setMediaController(mMediaController);
}
//监听MediaPlayer上报的错误信息
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
return false;
}
//Video播完的时候得到通知
@Override
public void onCompletion(MediaPlayer mp) {
this.finish();
}
//开始
public void onStart() {
// Play Video
mVideoView.setVideoURI(mUri);
mVideoView.requestFocus();
mVideoView.start();
super.onStart();
}
public void onPause() {
mPositionWhenPaused = mVideoView.getCurrentPosition();
mVideoView.stopPlayback();
super.onPause();
}
public void onResume() {
// Resume video player
if(mPositionWhenPaused >= 0) {
mVideoView.seekTo(mPositionWhenPaused);
mPositionWhenPaused = -1;
}
super.onResume();
}
}
如果想简单的播放视频文件,用上面的代码就可以了,我要说的是在使用过程中遇到的问题!
1、当布局文件中的SurfaceView和VideoView这两个控件的顺序颠倒以后就会出现看不到视频的问题,或者将SurfaceView设置为不可见。如果你先用了VideoView 然后又要用SurfaceView ,那么只要将VideoView隐藏就可以了,否则在SurfaceView上画的东西是看不到的。
2、 Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.movie);//只能获得res下的文件。如果哪位大侠知道如何获得assets的Uri 请留言 谢谢。