Android多媒体教程之播放视频的四种方法
程序员文章站
2022-06-11 09:33:09
本文主要给大家介绍的是关于android播放视频的四种方法,分享出来供大家参考学习,下面来一起看看详细的介绍:
一、通过intent的方式,调用系统自带的播放器...
本文主要给大家介绍的是关于android播放视频的四种方法,分享出来供大家参考学习,下面来一起看看详细的介绍:
一、通过intent的方式,调用系统自带的播放器
uri uri = uri.parse("/storage/emulated/0/dcim/camera/20170521_200117.mp4"); //调用系统自带的播放器 intent intent = new intent(intent.action_view); intent.setdataandtype(uri, "/storage/emulated/0/dcim/camera/20170521_200117.mp4"); startactivity(intent);
二、使用videoview
布局文件
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_video_play_by_vv" android:layout_width="match_parent" android:layout_height="match_parent"> <videoview android:id="@+id/video_view" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </relativelayout>
activity
public class videoplaybyvvactivity extends appcompatactivity { private videoview mvideoview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // requestwindowfeature(window.feature_no_title); //去掉 title // setrequestedorientation(activityinfo.screen_orientation_landscape); //设置全屏 // getwindow().addflags(windowmanager.layoutparams.flag_keep_screen_on); //设置屏幕常亮 setcontentview(r.layout.activity_video_play_by_vv); mvideoview = (videoview) findviewbyid(r.id.video_view); init(); } private void init() { string path = "/storage/emulated/0/dcim/camera/20170521_200117.mp4"; uri uri = uri.parse(path); mvideoview.setvideopath(path); mvideoview.start(); mvideoview.requestfocus(); } }
三、mediaplayer + surfaceview
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_video_play_by_sur" android:layout_width="match_parent" android:layout_height="match_parent"> <surfaceview android:id="@+id/surface_view" android:layout_width="180dp" android:layout_height="wrap_content"/> <linearlayout android:layout_alignparentbottom="true" android:layout_width="match_parent" android:layout_height="40dp"> <button android:id="@+id/stop" android:text="stop" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <button android:id="@+id/play" android:text="play" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <button android:id="@+id/pasue" android:text="pasue" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> </linearlayout> </relativelayout>
activity
public class videoplaybysuractivity extends appcompatactivity implements view.onclicklistener { private surfaceview msurfaceview; private mediaplayer mmediaplayer; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_video_play_by_sur); msurfaceview = (surfaceview) findviewbyid(r.id.surface_view); findviewbyid(r.id.stop).setonclicklistener(this); findviewbyid(r.id.pasue).setonclicklistener(this); findviewbyid(r.id.play).setonclicklistener(this); init(); } private void init() { mmediaplayer = new mediaplayer(); msurfaceview.getholder().addcallback(new surfaceholder.callback() { @override public void surfacecreated(surfaceholder holder) { play(); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { } @override public void surfacedestroyed(surfaceholder holder) { } }); } @override public void onclick(view v) { switch (v.getid()){ case r.id.stop: stop(); break; case r.id.play: if(!mmediaplayer.isplaying()){ play(); } break; case r.id.pasue: pasue(); break; } } public void stop(){ if(mmediaplayer.isplaying()){ mmediaplayer.stop(); } } public void pasue(){ if(mmediaplayer.isplaying()){ mmediaplayer.pause(); }else{ mmediaplayer.start(); } } public void play(){ string path = "/storage/emulated/0/dcim/camera/20170521_200117.mp4"; try { mmediaplayer.reset(); mmediaplayer.setaudiostreamtype(audiomanager.stream_music); //设置需要播放的视频 mmediaplayer.setdatasource(this, uri.parse(path)); //把视频画面输出到surfaceview mmediaplayer.setdisplay(msurfaceview.getholder()); mmediaplayer.prepare(); mmediaplayer.start(); } catch (ioexception e) { e.printstacktrace(); } } }
四、 mediaplayer + textureview
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_video_play_by_textrue_view" android:layout_width="match_parent" android:layout_height="match_parent"> <textureview android:id="@+id/texture_view" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <imageview android:id="@+id/video_image" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/all_darkbackground"/> </relativelayout>
activity
public class videoplaybytextrueviewactivity extends appcompatactivity implements mediaplayer.onpreparedlistener, mediaplayer.oninfolistener, mediaplayer.onbufferingupdatelistener { private textureview mtextureview; private imageview mimagevideo; private surface msurface; private mediaplayer mmediaplayer; private static string path = "/storage/emulated/0/dcim/camera/20170521_200117.mp4"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_video_play_by_textrue_view); mtextureview = (textureview) findviewbyid(r.id.texture_view); mimagevideo = (imageview) findviewbyid(r.id.video_image); init(); } private void init() { mtextureview.setsurfacetexturelistener(new textureview.surfacetexturelistener() { @override public void onsurfacetextureavailable(surfacetexture surfacetexture, int width, int height) { msurface = new surface(surfacetexture); log.e("tag", "---- onsurfacetextureavailable"); play(); } @override public void onsurfacetexturesizechanged(surfacetexture surface, int width, int height) { log.e("tag", "---- onsurfacetexturesizechanged"); } @override public boolean onsurfacetexturedestroyed(surfacetexture surface) { mtextureview=null; msurface=null; mmediaplayer.stop(); mmediaplayer.release(); return false; } @override public void onsurfacetextureupdated(surfacetexture surface) { } }); } public void play(){ mmediaplayer = new mediaplayer(); try { mmediaplayer.setdatasource(getapplicationcontext(), uri.parse(path)); mmediaplayer.setsurface(msurface); mmediaplayer.setaudiostreamtype(audiomanager.stream_music); mmediaplayer.setonpreparedlistener(this); mmediaplayer.setoninfolistener(this); mmediaplayer.setonbufferingupdatelistener(this); mmediaplayer.prepareasync(); } catch (ioexception e) { e.printstacktrace(); } } @override public void onprepared(mediaplayer mp) { mimagevideo.setvisibility(view.gone); mmediaplayer.start(); } @override public boolean oninfo(mediaplayer mp, int what, int extra) { return false; } @override public void onbufferingupdate(mediaplayer mp, int percent) { } }
参考文章
android 5.0(lollipop)中的surfacetexture,textureview, surfaceview和glsurfaceview
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
上一篇: Java 垃圾回收原理
推荐阅读
-
Android多媒体教程之播放视频的四种方法
-
Android编程实现播放视频的方法示例
-
Android提高之MediaPlayer播放网络视频的实现方法
-
RTSP播放器网页web无插件直播流媒体音视频播放器EasyPlayer-RTSP-Android解码获取视频帧的方法
-
解决Android使用MediaPlayer只播放视频不播放音频的方法
-
Android多媒体教程之播放视频的四种方法
-
Android提高之MediaPlayer播放网络视频的实现方法
-
Android学习笔记—— 十、 多媒体的基础使用 - 播放音频和视频
-
解决Android使用MediaPlayer只播放视频不播放音频的方法