本文来自http://blog.csdn.net/hellogv/,引用必须注明出处!
以前曾经地介绍过MediaPlayer的基本用法,这里就深入地讲解MediaPlayer的在线播放功能。本文主要实现MediaPlayer在线播放音频的功能,由于在线视频播放比在线音频播放复杂,因此先介绍在线音频播放的实现,这样可以帮助大家逐步深入了解MediaPlayer的在线播放功能。先来看看本文程序运行的结果:
main.xml的源码如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="fill_parent"android:layout_width="fill_parent">
- <LinearLayoutandroid:layout_height="wrap_content"
- android:layout_width="fill_parent"android:orientation="vertical"
- android:layout_gravity="top">
- <LinearLayoutandroid:orientation="horizontal"
- android:layout_gravity="center_horizontal"android:layout_marginTop="4.0dip"
- android:layout_height="wrap_content"android:layout_width="wrap_content">
- <Buttonandroid:layout_width="wrap_content"
- android:layout_height="wrap_content"android:id="@+id/btnPlayUrl"
- android:text="播放网络音频"></Button>
- <Buttonandroid:layout_height="wrap_content"android:id="@+id/btnPause"
- android:text="暂停"android:layout_width="80dip"></Button>
- <Buttonandroid:layout_height="wrap_content"
- android:layout_width="80dip"android:text="停止"android:id="@+id/btnStop"></Button>
- </LinearLayout>
- <LinearLayoutandroid:orientation="horizontal"
- android:layout_width="fill_parent"android:layout_height="wrap_content"
- android:layout_marginBottom="20dip">
- <SeekBarandroid:paddingRight="10dip"android:layout_gravity="center_vertical"
- android:paddingLeft="10dip"android:layout_weight="1.0"
- android:layout_height="wrap_content"android:layout_width="wrap_content"
- android:id="@+id/skbProgress"android:max="100"></SeekBar>
- </LinearLayout>
- </LinearLayout>
- </FrameLayout>
Player.java是本文的核心,Player.java实现了“进度条更新”、“数据缓冲”等功能,虽然不是很复杂的功能,但却是非常有用的功能。Player.java源码如下:
- packagecom.musicplayer;
- importjava.io.IOException;
- importjava.util.Timer;
- importjava.util.TimerTask;
- importandroid.media.AudioManager;
- importandroid.media.MediaPlayer;
- importandroid.media.MediaPlayer.OnBufferingUpdateListener;
- importandroid.media.MediaPlayer.OnCompletionListener;
- importandroid.os.Handler;
- importandroid.os.Message;
- importandroid.util.Log;
- importandroid.widget.SeekBar;
- publicclassPlayerimplementsOnBufferingUpdateListener,
- OnCompletionListener,MediaPlayer.OnPreparedListener{
- publicMediaPlayermediaPlayer;
- privateSeekBarskbProgress;
- privateTimermTimer=newTimer();
- publicPlayer(SeekBarskbProgress)
- {
- this.skbProgress=skbProgress;
- try{
- mediaPlayer=newMediaPlayer();
- mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- mediaPlayer.setOnBufferingUpdateListener(this);
- mediaPlayer.setOnPreparedListener(this);
- }catch(Exceptione){
- Log.e("mediaPlayer","error",e);
- }
- mTimer.schedule(mTimerTask,0,1000);
- }
- /*******************************************************
- *通过定时器和Handler来更新进度条
- ******************************************************/
- TimerTaskmTimerTask=newTimerTask(){
- @Override
- publicvoidrun(){
- if(mediaPlayer==null)
- return;
- if(mediaPlayer.isPlaying()&&skbProgress.isPressed()==false){
- handleProgress.sendEmptyMessage(0);
- }
- }
- };
- HandlerhandleProgress=newHandler(){
- publicvoidhandleMessage(Messagemsg){
- intposition=mediaPlayer.getCurrentPosition();
- intduration=mediaPlayer.getDuration();
- if(duration>0){
- longpos=skbProgress.getMax()*position/duration;
- skbProgress.setProgress((int)pos);
- }
- };
- };
- //*****************************************************
- publicvoidplay()
- {
- mediaPlayer.start();
- }
- publicvoidplayUrl(StringvideoUrl)
- {
- try{
- mediaPlayer.reset();
- mediaPlayer.setDataSource(videoUrl);
- mediaPlayer.prepare();//prepare之后自动播放
- //mediaPlayer.start();
- }catch(IllegalArgumentExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }catch(IllegalStateExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- }
- publicvoidpause()
- {
- mediaPlayer.pause();
- }
- publicvoidstop()
- {
- if(mediaPlayer!=null){
- mediaPlayer.stop();
- mediaPlayer.release();
- mediaPlayer=null;
- }
- }
- @Override
- /**
- *通过onPrepared播放
- */
- publicvoidonPrepared(MediaPlayerarg0){
- arg0.start();
- Log.e("mediaPlayer","onPrepared");
- }
- @Override
- publicvoidonCompletion(MediaPlayerarg0){
- Log.e("mediaPlayer","onCompletion");
- }
- @Override
- publicvoidonBufferingUpdate(MediaPlayerarg0,intbufferingProgress){
- skbProgress.setSecondaryProgress(bufferingProgress);
- intcurrentProgress=skbProgress.getMax()*mediaPlayer.getCurrentPosition()/mediaPlayer.getDuration();
- Log.e(currentProgress+"%play",bufferingProgress+"%buffer");
- }
- }
test_musicplayer.java是主程序,负责调用Player类,其中关键部分是SeekBarChangeEvent这个SeekBar拖动的事件:SeekBar的Progress是0~SeekBar.getMax()之内的数,而MediaPlayer.seekTo()的参数是0~MediaPlayer.getDuration()之内数,所以MediaPlayer.seekTo()的参数是(progress/seekBar.getMax())*player.mediaPlayer.getDuration()。
test_musicplayer.java源码如下:
- packagecom.musicplayer;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- importandroid.widget.SeekBar;
- publicclasstest_musicplayerextendsActivity{
- privateButtonbtnPause,btnPlayUrl,btnStop;
- privateSeekBarskbProgress;
- privatePlayerplayer;
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this.setTitle("在线音乐播放---hellogv编写");
- btnPlayUrl=(Button)this.findViewById(R.id.btnPlayUrl);
- btnPlayUrl.setOnClickListener(newClickEvent());
- btnPause=(Button)this.findViewById(R.id.btnPause);
- btnPause.setOnClickListener(newClickEvent());
- btnStop=(Button)this.findViewById(R.id.btnStop);
- btnStop.setOnClickListener(newClickEvent());
- skbProgress=(SeekBar)this.findViewById(R.id.skbProgress);
- skbProgress.setOnSeekBarChangeListener(newSeekBarChangeEvent());
- player=newPlayer(skbProgress);
- }
- classClickEventimplementsOnClickListener{
- @Override
- publicvoidonClick(Viewarg0){
- if(arg0==btnPause){
- player.pause();
- }elseif(arg0==btnPlayUrl){
- //在百度MP3里随便搜索到的,大家可以试试别的链接
- Stringurl="http://219.138.125.22/myweb/mp3/CMP3/JH19.MP3";
- player.playUrl(url);
- }elseif(arg0==btnStop){
- player.stop();
- }
- }
- }
- classSeekBarChangeEventimplementsSeekBar.OnSeekBarChangeListener{
- intprogress;
- @Override
- publicvoidonProgressChanged(SeekBarseekBar,intprogress,
- booleanfromUser){
- //原本是(progress/seekBar.getMax())*player.mediaPlayer.getDuration()
- this.progress=progress*player.mediaPlayer.getDuration()
- /seekBar.getMax();
- }
- @Override
- publicvoidonStartTrackingTouch(SeekBarseekBar){
- }
- @Override
- publicvoidonStopTrackingTouch(SeekBarseekBar){
- //seekTo()的参数是相对与影片时间的数字,而不是与seekBar.getMax()相对的数字
- player.mediaPlayer.seekTo(progress);
- }
- }
- }