Android 播放视频的四种方法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u011403718/article/details/48287281

之前的工作工作中曾经接触过一点视频播放方面的内容,最近又有所接触,索性将几种方法总结下

Android播放视频四种方法:

1、调用本地播放器播放视频

2、mediaplayer+surfaceview

3、利用videoview控件

4、webview网络播放视频


1、调用本地播放器播放视频


<pre name="code" class="java">String path = ""; //本地视频文件路径
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+path); 
//调用系统自带的播放器
 Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(uri, "video/mp4");
 startActivity(intent);



2mediaplayer+surfaceview

public class MediaPlayerActivity extends Activity implements OnClickListener,SurfaceHolder.Callback{	
	private Button mButton1,mButton2,mButton3,mButton4,mButton5,mButton6;
	private String path; 
	private SurfaceView surface;
	private SurfaceHolder surfaceHolder;
	private MediaPlayer mediaPlayer;
	private int prosition=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_media_player);
		init();
		lis();
		path ="http://192.168.1.100/wuxin.mp4";		
		//mediaplayer + surfaceView
		surfaceHolder = surface.getHolder();
		//设置分辨率,不设置为默认值
		// surfaceHolder.setFixedSize(176, 144);
		surfaceHolder.addCallback(this);    //对surface对象添加监听事件
		//设置surfaceview不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		mediaPlayer = new MediaPlayer();
		
	}
	private void lis() {
		mButton1.setOnClickListener(this);
		mButton2.setOnClickListener(this);
		mButton3.setOnClickListener(this);
		mButton4.setOnClickListener(this);
		mButton5.setOnClickListener(this);
		mButton6.setOnClickListener(this);
		
	}
	private void init() {
		mVideoView = (VideoView)findViewById(R.id.videoview);
		surface = (SurfaceView)findViewById(R.id.surfaceview);
		mButton1 = (Button)findViewById(R.id.button1);
		mButton2 = (Button)findViewById(R.id.button2);
		mButton3 = (Button)findViewById(R.id.button3);
		mButton4 = (Button)findViewById(R.id.button4);
		mButton5 = (Button)findViewById(R.id.button5);
		mButton6 = (Button)findViewById(R.id.button6);
	}
	@Override
	public void onClick(View view) {
		switch(view.getId()){
		case R.id.button1:
			break;
		case R.id.button2:
			break;
		case R.id.button3:  //播放
			play();
			break;
		case R.id.button4: //停止
			if(mediaPlayer.isPlaying()){
				mediaPlayer.stop();
			}
			break;
		case R.id.button5: //暂停
			if(mediaPlayer.isPlaying()){
				mediaPlayer.pause();
			}else{
				mediaPlayer.start();
			}
			break;
		case R.id.button6:   //重置
			if(mediaPlayer.isPlaying()){
				mediaPlayer.seekTo(0);
			}else{
				play();
			}
			break;
		default :
			break;
		}
	}
	private void play() {
		mediaPlayer.reset();
		try {
			mediaPlayer.setDataSource(path);
			mediaPlayer.setDisplay(surfaceHolder);
			mediaPlayer.prepare();
			mediaPlayer.start();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		 if(prosition>0){
             play();
             mediaPlayer.seekTo(prosition);
             prosition=0;
         }
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		if(mediaPlayer.isPlaying()){
            prosition=mediaPlayer.getCurrentPosition();
            mediaPlayer.stop();
        }		
	}
}



3、利用videoview控件

videoview是Android自带的一个控件,它继承surfaceview,使用MediaPlayer来做播放视频的类,可以把vedeoview可以看成是一个封装好的surfaceview,可以直接调用视频的播放,停止等等接口。

通过VideoView播放视频的步骤:

 1、在界面布局文件中定义VideoView组件,或在程序中创建VideoView组件

 2、调用VideoView的如下两个方法来加载指定的视频

         setVidePath(String path):加载path文件代表的视频

         setVideoURI(Uri uri):加载uri所对应的视频

3、调用VideoView的start()、stop()、psuse()方法来控制视频的播放

VideoView通过与MediaController类结合使用,开发者可以不用自己控制播放与暂停,加载进度条和控制按钮

<span style="white-space:pre">		</span>String path ="http://192.168.1.100/wuxin.mp4"; //网络视频地址,也可以用本地文件uri
		mVideoView.setVideoPath(path);
		mVideoView.setMediaController(new MediaController(MediaPlayerActivity.this));//<span style="font-family: Arial; font-size: 14px; line-height: 26px; white-space: pre;">加载进度条和控制按钮</span>
		mVideoView.requestFocus();
                mVideoView.start();

4、webview网络播放视频

webView = (WebView) this.findViewById(R.id.webView1);
  // 设置加载进来的页面自适应手机屏幕
  WebSettings settings = webView.getSettings();
  settings.setUseWideViewPort(true);
  settings.setLoadWithOverviewMode(true);
 webView.loadUrl("http://www.baidu.com");
 }


展开阅读全文

没有更多推荐了,返回首页