android学习之视频播放

果断这几篇文章都是写的多媒体的,这篇文章就谈谈如果使用VideoView来播放视频,其实感觉和播放音乐差不多的,直接贴代码,因为感觉也蛮简单的,对了,,在后面我继承了VideoView写了一个自己的VideoView,这样我们方便设置Video来适配自己的屏幕。

package com.example.videoviewdemo;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity {
	// declar a VideoView
	private VideoView myVideoView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		myVideoView = (VideoView) findViewById(R.id.my_video);
		// get the path
		String path = "/sdcard/fcar.3gp";
		// set the path for VideoView
		myVideoView.setVideoPath(path);
		// Uri
		// MyUri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.aa);
		// myVideoView.setVideoURI(MyUri);
		// MediaController is used for us to control the progress
		MediaController mc = new MediaController(this);
		// bind the MediaController with the VideoView
		myVideoView.setMediaController(mc);
		// get the focus
		myVideoView.requestFocus();
		// play the Video
		myVideoView.start();

	}

}

package com.example.videoviewdemo;


import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;


public class MyVideoView extends VideoView {
// those three constructor will be use for the different way to create view
public MyVideoView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}


public MyVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}


public MyVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
// measure the width of the screen
int width = getDefaultSize(0, widthMeasureSpec);
// measure the height of the screen
int height = getDefaultSize(0, heightMeasureSpec);
// set the size that we use to play the video
setMeasuredDimension(width, height);
}


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值