Android开发实现的简单媒体播放器功能示例
本文实例讲述了Android开发实现的简单媒体播放器功能。分享给大家供大家参考,具体如下:
一、概述:
因为播放视频需要很大的内存,所以必须使用surfaceview,
surfaceview里实现了双缓冲的功能,
二、全部代码:
/** *@描述使用surfaceview创建视频 *@项目名称App_Basic *@包名com.example.basic.media *@类名MediaPlayerActivity *@authorchenlin *@date2013-3-25上午7:36:32 *@version1.0 */ publicclassMediaPlayerActivityextendsBaseActivity{ privateMediaPlayermPlayer; privateButtonmBtnPlay; privateButtonmBtnPause; privateSurfaceViewmSurfaceView; privateintmCurrentPosition; privateSeekBarseekBar; privatebooleanisPlaying; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); mSurfaceView=newSurfaceView(this); seekBar=newSeekBar(this); seekBar.setOnSeekBarChangeListener(newOnSeekBarChangeListener(){ @Override publicvoidonStopTrackingTouch(SeekBarseekBar){ intprogress=seekBar.getProgress(); if(mPlayer!=null&&isPlaying){ mPlayer.seekTo(progress); } } @Override publicvoidonStartTrackingTouch(SeekBarseekBar){ //TODOAuto-generatedmethodstub } @Override publicvoidonProgressChanged(SeekBarseekBar,intprogress,booleanfromUser){ //TODOAuto-generatedmethodstub } }); mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mSurfaceView.getHolder().addCallback(newCallback(){ @Override publicvoidsurfaceDestroyed(SurfaceHolderholder){ if(mPlayer!=null&&mPlayer.isPlaying()){ mCurrentPosition=mPlayer.getCurrentPosition(); stop(); } } @Override publicvoidsurfaceCreated(SurfaceHolderholder){ if(mCurrentPosition>0){ play(mCurrentPosition); } } @Override publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,intheight){ //TODOAuto-generatedmethodstub } }); } publicvoidplay(finalintcurrentPosition){ try{ mPlayer=newMediaPlayer(); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); UrimyUri=Uri.fromFile(newFile("sdcard/mp3/1.mp3")); mPlayer.setDataSource(getApplicationContext(),myUri); mPlayer.setLooping(true); mPlayer.setDisplay(mSurfaceView.getHolder()); mPlayer.prepareAsync(); //mPlayer.prepare(); mBtnPlay.setEnabled(false); mPlayer.setOnCompletionListener(newOnCompletionListener(){ @Override publicvoidonCompletion(MediaPlayermp){ mBtnPlay.setEnabled(true); mPlayer.release(); } }); mPlayer.setOnErrorListener(newOnErrorListener(){ @Override publicbooleanonError(MediaPlayermp,intwhat,intextra){ mBtnPlay.setEnabled(true); mPlayer.release(); isPlaying=false; returnfalse; } }); mPlayer.setOnPreparedListener(newOnPreparedListener(){ publicvoidonPrepared(MediaPlayermp){ mPlayer.start(); mPlayer.seekTo(currentPosition); seekBar.setMax(mPlayer.getDuration()); newThread(newRunnable(){ @Override publicvoidrun(){ isPlaying=true; while(isPlaying){ seekBar.setProgress(mPlayer.getCurrentPosition()); SystemClock.sleep(500); } } }); } }); }catch(Exceptione){ e.printStackTrace(); } } publicvoidpause(){ if("接续".equals(mBtnPause.getText().toString())){ mBtnPause.setText("暂停"); mPlayer.start(); return; } if(mPlayer!=null&&mPlayer.isPlaying()){ mPlayer.pause(); mBtnPause.setText("继续"); return; } } publicvoidreset(){ if(mPlayer!=null&&mPlayer.isPlaying()){ mPlayer.seekTo(0); return; } play(0); } publicvoidstop(){ 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程序设计有所帮助。