Android开发实现的简单媒体播放器功能示例
程序员文章站
2023-12-16 11:30:28
本文实例讲述了android开发实现的简单媒体播放器功能。分享给大家供大家参考,具体如下:
一、概述:
因为播放视频需要很大的内存,所以必须使用surfaceview...
本文实例讲述了android开发实现的简单媒体播放器功能。分享给大家供大家参考,具体如下:
一、概述:
因为播放视频需要很大的内存,所以必须使用surfaceview ,
surfaceview 里实现了双缓冲的功能,
二、全部代码:
/** * @描述 使用surfaceview 创建视频 * @项目名称 app_basic * @包名 com.example.basic.media * @类名 mediaplayeractivity * @author chenlin * @date 2013-3-25 上午7:36:32 * @version 1.0 */ public class mediaplayeractivity extends baseactivity { private mediaplayer mplayer; private button mbtnplay; private button mbtnpause; private surfaceview msurfaceview; private int mcurrentposition; private seekbar seekbar; private boolean isplaying; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); msurfaceview = new surfaceview(this); seekbar = new seekbar(this); seekbar.setonseekbarchangelistener(new onseekbarchangelistener() { @override public void onstoptrackingtouch(seekbar seekbar) { int progress = seekbar.getprogress(); if (mplayer != null && isplaying) { mplayer.seekto(progress); } } @override public void onstarttrackingtouch(seekbar seekbar) { // todo auto-generated method stub } @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { // todo auto-generated method stub } }); msurfaceview.getholder().settype(surfaceholder.surface_type_push_buffers); msurfaceview.getholder().addcallback(new callback() { @override public void surfacedestroyed(surfaceholder holder) { if (mplayer!= null && mplayer.isplaying()) { mcurrentposition = mplayer.getcurrentposition(); stop(); } } @override public void surfacecreated(surfaceholder holder) { if (mcurrentposition > 0) { play(mcurrentposition); } } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { // todo auto-generated method stub } }); } public void play(final int currentposition){ try { mplayer = new mediaplayer(); mplayer.setaudiostreamtype(audiomanager.stream_music); uri myuri = uri.fromfile(new file("sdcard/mp3/1.mp3")); mplayer.setdatasource(getapplicationcontext(), myuri); mplayer.setlooping(true); mplayer.setdisplay(msurfaceview.getholder()); mplayer.prepareasync(); //mplayer.prepare(); mbtnplay.setenabled(false); mplayer.setoncompletionlistener(new oncompletionlistener() { @override public void oncompletion(mediaplayer mp) { mbtnplay.setenabled(true); mplayer.release(); } }); mplayer.setonerrorlistener(new onerrorlistener() { @override public boolean onerror(mediaplayer mp, int what, int extra) { mbtnplay.setenabled(true); mplayer.release(); isplaying = false; return false; } }); mplayer.setonpreparedlistener(new onpreparedlistener() { public void onprepared(mediaplayer mp) { mplayer.start(); mplayer.seekto(currentposition); seekbar.setmax(mplayer.getduration()); new thread(new runnable() { @override public void run() { isplaying = true; while(isplaying){ seekbar.setprogress(mplayer.getcurrentposition()); systemclock.sleep(500); } } }); } }); } catch (exception e) { e.printstacktrace(); } } public void pause(){ if ("接续".equals(mbtnpause.gettext().tostring())) { mbtnpause.settext("暂停"); mplayer.start(); return; } if (mplayer != null && mplayer.isplaying()) { mplayer.pause(); mbtnpause.settext("继续"); return; } } public void reset(){ if (mplayer != null && mplayer.isplaying()) { mplayer.seekto(0); return; } play(0); } public void stop(){ if (mplayer != null && mplayer.isplaying()) { mplayer.stop(); mplayer.release(); mplayer = null; mbtnplay.setenabled(true); isplaying = false; } } }
更多关于android相关内容感兴趣的读者可查看本站专题:《android多媒体操作技巧汇总(音频,视频,录音等)》、《android开发入门与进阶教程》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android文件操作技巧汇总》、《android资源操作技巧汇总》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。