Android编程视频播放API之MediaPlayer用法示例
程序员文章站
2023-12-02 21:58:10
本文实例讲述了android编程视频播放api之mediaplayer用法。分享给大家供大家参考,具体如下:
现在项目要做音视频这块功能,这正是我相对比较薄弱的地方,项目...
本文实例讲述了android编程视频播放api之mediaplayer用法。分享给大家供大家参考,具体如下:
现在项目要做音视频这块功能,这正是我相对比较薄弱的地方,项目中要用到,赶紧抓紧时间学习。前面有一编介绍不错的文章,值得学习。
这里给出一个较为简单的示例:
/** * android api mediaplayer示例 * * @description: * @author ldm * @date 2016-4-20 上午10:42:06 */ public class mediaplayeractivity extends activity implements onclicklistener { private button mlocalvideo; private button mstreamvideo; private button mlocalaudio; private button mresourcesaudio; private static final string media = "media"; private static final int local_audio = 1; private static final int resources_audio = 3; private static final int stream_video = 5; @override protected void oncreate(bundle icicle) { super.oncreate(icicle); setcontentview(r.layout.activity_mediaplayer); initviewsandevents(); } private void initviewsandevents() { mlocalaudio = (button) findviewbyid(r.id.localaudio); mresourcesaudio = (button) findviewbyid(r.id.resourcesaudio); mlocalvideo = (button) findviewbyid(r.id.localvideo); mstreamvideo = (button) findviewbyid(r.id.streamvideo); mlocalaudio.setonclicklistener(this); mresourcesaudio.setonclicklistener(this); mlocalvideo.setonclicklistener(this); mstreamvideo.setonclicklistener(this); } @override public void onclick(view v) { switch (v.getid()) { case r.id.localaudio: case r.id.localvideo: intent intent = new intent(mediaplayeractivity.this.getapplication(), mediaplayerdemo_audio.class); intent.putextra(media, local_audio); startactivity(intent); break; case r.id.resourcesaudio: intent resin = new intent(mediaplayeractivity.this.getapplication(), mediaplayerdemo_audio.class); resin.putextra(media, resources_audio); startactivity(resin); break; case r.id.streamvideo: intent streamin = new intent(mediaplayeractivity.this, mediaplayerdemo_video.class); streamin.putextra(media, stream_video); startactivity(streamin); break; } } }
/** * onbufferingupdatelistener可以获取视频音频流的缓冲状态; * oncompletionlistener播放完毕监听器 * onpreparedlistener准备播放相关接口 * onvideosizechangedlistener注册视频大小改变监听器 * surfaceholder.callback:surfaceview监听callback * @description: * @author ldm * @date 2016-4-20 上午9:46:16 */ public class mediaplayer_video extends activity implements onbufferingupdatelistener, oncompletionlistener, onpreparedlistener, onvideosizechangedlistener, surfaceholder.callback { private static final string tag = "mediaplayerdemo"; //video宽度 private int mvideowidth; //video高度 private int mvideoheight; //播放器mediaplayer private mediaplayer mmediaplayer; //播放界面surfaceview private surfaceview mpreview; //媒体播放器的载体 surfaceholder private surfaceholder holder; //文件路径 private string path; //bundle数据对象 private bundle extras; private static final string media = "media"; private static final int local_video = 1; private static final int resources_audio = 3; private static final int stream_video = 5; private boolean misvideosizeknown = false; private boolean misvideoreadytobeplayed = false; @suppresswarnings("deprecation") @override public void oncreate(bundle icicle) { super.oncreate(icicle); setcontentview(r.layout.mediaplayer_2); mpreview = (surfaceview) findviewbyid(r.id.surface); holder = mpreview.getholder(); holder.addcallback(this); //高版本上已经不推荐使用了 ,如果要兼容低版本(如android 2.3或以下版本)要加上这段代码,不然播放时 只会有声音 没有图像。 holder.settype(surfaceholder.surface_type_push_buffers); extras = getintent().getextras(); } private void playvideo(integer media) { docleanup(); try { switch (media) { case local_video://本地文件(如sd卡) /* * todo:将路径变量设置为本地媒体文件路径。 */ path = "";//这里要填写你所播放音视频的文件路径 if (path == "") { toast .maketext( mediaplayer_video.this, "please edit mediaplayerdemo_video activity, " + "and set the path variable to your media file path." + " your media file must be stored on sdcard.", toast.length_long).show(); } break; case stream_video://流文件 /** * 路径变量设置为mp4或3gpp格式的url。应该使用http协议。 * 媒体播放器只能通过流的形式获取数据,所以需要: * 1。必须是媒体流数据。 * 2。必须合理剪辑。 */ path = ""; if (path == "") { // tell the user to provide a media file url. toast .maketext( mediaplayer_video.this, "please edit mediaplayerdemo_video activity," + " and set the path variable to your media file url.", toast.length_long).show(); } break; case resources_audio: //播放放在诸如res/raw文件夹下的资源文件 //直接通过mmediaplayer = mediaplayer.create(this, r.raw.test_cbr); //可以参考api demos中audiofxdemo类文件 break; } // 创建mediaplayer对象,传入文件数据并设备监听 mmediaplayer = new mediaplayer(); mmediaplayer.setdatasource(path); mmediaplayer.setdisplay(holder); //使用 create() 方法创建的 mediaplayer, 直接指定了 媒体资源, 不需要再进行 调用 prepare() 方法; mmediaplayer.prepare(); mmediaplayer.setonbufferingupdatelistener(this); mmediaplayer.setoncompletionlistener(this); mmediaplayer.setonpreparedlistener(this); mmediaplayer.setonvideosizechangedlistener(this); //指定音频流类型 mmediaplayer.setaudiostreamtype(audiomanager.stream_music); } catch (exception e) { log.e(tag, "error: " + e.getmessage(), e); } } public void onbufferingupdate(mediaplayer arg0, int percent) { log.d(tag, "onbufferingupdate percent:" + percent); } public void oncompletion(mediaplayer arg0) { log.d(tag, "oncompletion called"); } public void onvideosizechanged(mediaplayer mp, int width, int height) { log.v(tag, "onvideosizechanged called"); if (width == 0 || height == 0) { log.e(tag, "invalid video width(" + width + ") or height(" + height + ")"); return; } misvideosizeknown = true; mvideowidth = width; mvideoheight = height; if (misvideoreadytobeplayed && misvideosizeknown) { startvideoplayback(); } } public void onprepared(mediaplayer mediaplayer) { log.d(tag, "onprepared called"); misvideoreadytobeplayed = true; if (misvideoreadytobeplayed && misvideosizeknown) { startvideoplayback(); } } public void surfacechanged(surfaceholder surfaceholder, int i, int j, int k) { log.d(tag, "surfacechanged called"); } public void surfacedestroyed(surfaceholder surfaceholder) { log.d(tag, "surfacedestroyed called"); } public void surfacecreated(surfaceholder holder) { log.d(tag, "surfacecreated called"); playvideo(extras.getint(media)); } @override protected void onpause() { super.onpause(); releasemediaplayer(); docleanup(); } @override protected void ondestroy() { super.ondestroy(); releasemediaplayer(); docleanup(); } private void releasemediaplayer() { if (mmediaplayer != null) { mmediaplayer.release(); mmediaplayer = null; } } private void docleanup() { mvideowidth = 0; mvideoheight = 0; misvideoreadytobeplayed = false; misvideosizeknown = false; } private void startvideoplayback() { log.v(tag, "startvideoplayback"); holder.setfixedsize(mvideowidth, mvideoheight); mmediaplayer.start(); } }
布局文件activity_mediaplayer
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <button android:id="@+id/localvideo" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@string/local_video" /> <button android:id="@+id/streamvideo" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@string/stream_video" /> <button android:id="@+id/localaudio" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@string/local_audio" /> <button android:id="@+id/resourcesaudio" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@string/res_audio" /> </linearlayout>
布局文件activity_mediaplayer_video
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <surfaceview android:id="@+id/surface" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"> </surfaceview> </linearlayout>
更多关于android相关内容感兴趣的读者可查看本站专题:《android多媒体操作技巧汇总(音频,视频,录音等)》、《android开发入门与进阶教程》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android数据库操作技巧总结》、《android文件操作技巧汇总》、《android资源操作技巧汇总》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。