您的位置:首页 > 移动开发 > Android开发

android学习之视频播放

2016-04-15 16:59 363 查看
果断这几篇文章都是写的多媒体的,这篇文章就谈谈如果使用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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: