android学习之视频播放

来源:互联网 发布:2016年网络搜索话题 编辑:程序博客网 时间:2024/05/17 06:54

果断这几篇文章都是写的多媒体的,这篇文章就谈谈如果使用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 VideoViewprivate VideoView myVideoView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myVideoView = (VideoView) findViewById(R.id.my_video);// get the pathString path = "/sdcard/fcar.3gp";// set the path for VideoViewmyVideoView.setVideoPath(path);// Uri// MyUri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.aa);// myVideoView.setVideoURI(MyUri);// MediaController is used for us to control the progressMediaController mc = new MediaController(this);// bind the MediaController with the VideoViewmyVideoView.setMediaController(mc);// get the focusmyVideoView.requestFocus();// play the VideomyVideoView.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);
}


}



0 0
原创粉丝点击