关闭

VideoView播放视频

1060人阅读 评论(0) 收藏 举报

<?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:gravity="center_vertical"
    android:orientation="vertical" >

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

</LinearLayout>

public class MediaActivity extends Activity {

	VideoView videoView;
	private int mPositionWhenPaused = -1;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// Create VideoView
		videoView = (VideoView) this.findViewById(R.id.videoView);
		// Create MediaController
		// MediaController mc = new MediaController(this);
		// Set MediaController to VideoView
		// videoView.setMediaController(mc);
		// Set video path of SD Card
		// videoView.setVideoURI(Uri.parse("file:///sdcard/samplemp4.mp4"));
		// Set video web path
		// videoView.setVideoURI(Uri.parse("http://www.xyz.com/../sample3gp.3gp"));
		videoView.setVideoURI(Uri.parse("android.resource://"
				+ getPackageName() + "/" + R.raw.media2));
		// Set requestFocus
		videoView.requestFocus();
		videoView
				.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

					@Override
					public void onCompletion(MediaPlayer arg0) {
						// TODO Auto-generated method stub
						// Intent intent=new
						// Intent(MediaActivity.this,AdsActivity.class);
						// startActivity(intent);
					}
				});

		// Play Video
		videoView.start();

	}

	public void onPause() {
		// 在活动时是停止视频的停顿.
		mPositionWhenPaused = videoView.getCurrentPosition();
		videoView.stopPlayback();

		super.onPause();
	}

	public void onResume() {
		// 恢复视频播放器
		if (mPositionWhenPaused >= 0) {
			videoView.seekTo(mPositionWhenPaused);
			mPositionWhenPaused = -1;
		}
		videoView.start();
		super.onResume();
	}
}



1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:87704次
    • 积分:1137
    • 等级:
    • 排名:千里之外
    • 原创:24篇
    • 转载:17篇
    • 译文:2篇
    • 评论:17条
    文章分类
    最新评论