Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android提高21篇之二十:MediaPlayer播放網絡音頻

Android提高21篇之二十:MediaPlayer播放網絡音頻

編輯:關於android開發

       以前曾經地介紹過MediaPlayer的基本用法,這裡就深入地講解MediaPlayer的在線播放功能。本文主要實現MediaPlayer在線播放音頻的功能,由於在線視頻播放比在線音頻播放復雜,因此先介紹在線音頻播放的實現,這樣可以幫助大家逐步深入了解MediaPlayer的在線播放功能。先來看看本文程序運行的結果:

      main.xml的源碼如下:

XML/HTML代碼
  1. <framelayout xmlns:android="http://schemas.android.com/apk/res/android"   
  2.         android:layout_height="fill_parent" android:layout_width="fill_parent">  
  3.         <linearlayout android:layout_height="wrap_content"   
  4.                 android:layout_width="fill_parent" android:orientation="vertical"  
  5.                 android:layout_gravity="top">  
  6.                 <linearlayout android:orientation="horizontal"   
  7.                         android:layout_gravity="center_horizontal" android:layout_marginTop="4.0dip"  
  8.                         android:layout_height="wrap_content" android:layout_width="wrap_content">  
  9.                         <button android:layout_width="wrap_content"   
  10.                                 android:layout_height="wrap_content" android:id="@+id/btnPlayUrl"  
  11.                                 android:text="播放網絡音頻">  
  12.                         <button android:layout_height="wrap_content" android:id="@+id/btnPause"   
  13.                                 android:text="暫停" android:layout_width="80dip">  
  14.                         <button android:layout_height="wrap_content"   
  15.                                 android:layout_width="80dip" android:text="停止" android:id="@+id/btnStop">  
  16.                   
  17.                 <linearlayout android:orientation="horizontal"   
  18.                         android:layout_width="fill_parent" android:layout_height="wrap_content"  
  19.                         android:layout_marginBottom="20dip">  
  20.                         <seekbar android:paddingright="10dip" android:layout_gravity="center_vertical"   
  21.                                 android:paddingLeft="10dip" android:layout_weight="1.0"  
  22.                                 android:layout_height="wrap_content" android:layout_width="wrap_content"  
  23.                                 android:id="@+id/skbProgress" android:max="100">  

       Player.java是本文的核心,Player.java實現了“進度條更新”、“數據緩沖”等功能,雖然不是很復雜的功能,但卻是非常有用的功能。Player.java源碼如下:

Java代碼
  1. package com.musicplayer;  
  2.   
  3.   
  4. import java.io.IOException;  
  5. import java.util.Timer;  
  6. import java.util.TimerTask;  
  7. import android.media.AudioManager;  
  8. import android.media.MediaPlayer;  
  9. import android.media.MediaPlayer.OnBufferingUpdateListener;  
  10. import android.media.MediaPlayer.OnCompletionListener;  
  11. import android.os.Handler;  
  12. import android.os.Message;  
  13. import android.util.Log;  
  14. import android.widget.SeekBar;  
  15.   
  16. public class Player implements OnBufferingUpdateListener,  
  17.                 OnCompletionListener, MediaPlayer.OnPreparedListener{  
  18.         public MediaPlayer mediaPlayer;  
  19.         private SeekBar skbProgress;  
  20.         private Timer mTimer=new Timer();  
  21.         public Player(SeekBar skbProgress)  
  22.         {  
  23.                 this.skbProgress=skbProgress;  
  24.                   
  25.                 try {  
  26.                         mediaPlayer = new MediaPlayer();  
  27.                         mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);  
  28.                         mediaPlayer.setOnBufferingUpdateListener(this);  
  29.                         mediaPlayer.setOnPreparedListener(this);  
  30.                 } catch (Exception e) {  
  31.                         Log.e("mediaPlayer", "error", e);  
  32.                 }  
  33.                   
  34.                 mTimer.schedule(mTimerTask, 0, 1000);  
  35.         }  
  36.           
  37.         /******************************************************* 
  38.          * 通過定時器和Handler來更新進度條 
  39.          ******************************************************/  
  40.         TimerTask mTimerTask = new TimerTask() {  
  41.                 @Override  
  42.                 public void run() {  
  43.                         if(mediaPlayer==null)  
  44.                                 return;  
  45.                         if (mediaPlayer.isPlaying() && skbProgress.isPressed() == false) {  
  46.                                 handleProgress.sendEmptyMessage(0);  
  47.                         }  
  48.                 }  
  49.         };  
  50.           
  51.         Handler handleProgress = new Handler() {  
  52.                 public void handleMessage(Message msg) {  
  53.   
  54.                         int position = mediaPlayer.getCurrentPosition();  
  55.                         int duration = mediaPlayer.getDuration();  
  56.                           
  57.                         if (duration > 0) {  
  58.                                 long pos = skbProgress.getMax() * position / duration;  
  59.                                 skbProgress.setProgress((int) pos);  
  60.                         }  
  61.                 };  
  62.         };  
  63.         //*****************************************************  
  64.           
  65.         public void play()  
  66.         {  
  67.                 mediaPlayer.start();  
  68.         }  
  69.           
  70.         public void playUrl(String videoUrl)  
  71.         {  
  72.                 try {  
  73.                         mediaPlayer.reset();  
  74.                         mediaPlayer.setDataSource(videoUrl);  
  75.                         mediaPlayer.prepare();//prepare之後自動播放  
  76.                         //mediaPlayer.start();  
  77.                 } catch (IllegalArgumentException e) {  
  78.                         // TODO Auto-generated catch block  
  79.                         e.printStackTrace();  
  80.                 } catch (IllegalStateException e) {  
  81.                         // TODO Auto-generated catch block  
  82.                         e.printStackTrace();  
  83.                 } catch (IOException e) {  
  84.                         // TODO Auto-generated catch block  
  85.                         e.printStackTrace();  
  86.                 }  
  87.         }  
  88.   
  89.           
  90.         public void pause()  
  91.         {  
  92.                 mediaPlayer.pause();  
  93.         }  
  94.           
  95.         public void stop()  
  96.         {  
  97.                 if (mediaPlayer != null) {   
  98.                         mediaPlayer.stop();  
  99.             mediaPlayer.release();   
  100.             mediaPlayer = null;   
  101.         }   
  102.         }  
  103.   
  104.         @Override  
  105.         /**  
  106.          * 通過onPrepared播放  
  107.          */  
  108.         public void onPrepared(MediaPlayer arg0) {  
  109.                 arg0.start();  
  110.                 Log.e("mediaPlayer", "onPrepared");  
  111.         }  
  112.   
  113.         @Override  
  114.         public void onCompletion(MediaPlayer arg0) {  
  115.                 Log.e("mediaPlayer", "onCompletion");  
  116.         }  
  117.   
  118.         @Override  
  119.         public void onBufferingUpdate(MediaPlayer arg0, int bufferingProgress) {  
  120.                 skbProgress.setSecondaryProgress(bufferingProgress);  
  121.                 int currentProgress=skbProgress.getMax()*mediaPlayer.getCurrentPosition()/mediaPlayer.getDuration();  
  122.                 Log.e(currentProgress+"% play", bufferingProgress + "% buffer");  
  123.         }  
  124.   
  125. }  

       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源碼如下:

Java代碼
  1. package com.musicplayer;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.SeekBar;  
  9.   
  10. public class test_musicplayer extends Activity {  
  11.         private Button btnPause, btnPlayUrl, btnStop;  
  12.         private SeekBar skbProgress;  
  13.         private Player player;  
  14.   
  15.         /** Called when the activity is first created. */  
  16.         @Override  
  17.         public void onCreate(Bundle savedInstanceState) {  
  18.                 super.onCreate(savedInstanceState);  
  19.                 setContentView(R.layout.main);  
  20.           
  21.                 this.setTitle("在線音樂播放---hellogv編寫");  
  22.   
  23.                 btnPlayUrl = (Button) this.findViewById(R.id.btnPlayUrl);  
  24.                 btnPlayUrl.setOnClickListener(new ClickEvent());  
  25.   
  26.                 btnPause = (Button) this.findViewById(R.id.btnPause);  
  27.                 btnPause.setOnClickListener(new ClickEvent());  
  28.   
  29.                 btnStop = (Button) this.findViewById(R.id.btnStop);  
  30.                 btnStop.setOnClickListener(new ClickEvent());  
  31.   
  32.                 skbProgress = (SeekBar) this.findViewById(R.id.skbProgress);  
  33.                 skbProgress.setOnSeekBarChangeListener(new SeekBarChangeEvent());  
  34.                 player = new Player(skbProgress);  
  35.   
  36.         }  
  37.   
  38.         class ClickEvent implements OnClickListener {  
  39.   
  40.                 @Override  
  41.                 public void onClick(View arg0) {  
  42.                         if (arg0 == btnPause) {  
  43.                                 player.pause();  
  44.                         } else if (arg0 == btnPlayUrl) {  
  45.                                 //在百度MP3裡隨便搜索到的,大家可以試試別的鏈接  
  46.                                 String url="http://219.138.125.22/myweb/mp3/CMP3/JH19.MP3";  
  47.                                 player.playUrl(url);  
  48.                         } else if (arg0 == btnStop) {  
  49.                                 player.stop();  
  50.                         }  
  51.                 }  
  52.         }  
  53.   
  54.         class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {  
  55.                 int progress;  
  56.   
  57.                 @Override  
  58.                 public void onProgressChanged(SeekBar seekBar, int progress,  
  59.                                 boolean fromUser) {  
  60.                         // 原本是(progress/seekBar.getMax())*player.mediaPlayer.getDuration()  
  61.                         this.progress = progress * player.mediaPlayer.getDuration()  
  62.                                         / seekBar.getMax();  
  63.                 }  
  64.   
  65.                 @Override  
  66.                 public void onStartTrackingTouch(SeekBar seekBar) {  
  67.   
  68.                 }  
  69.   
  70.                 @Override  
  71.                 public void onStopTrackingTouch(SeekBar seekBar) {  
  72.                         // seekTo()的參數是相對與影片時間的數字,而不是與seekBar.getMax()相對的數字  
  73.                         player.mediaPlayer.seekTo(progress);  
  74.                 }  
  75.         }  
  76.   
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved