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

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

編輯:關於android開發

       上次講解了MediaPlayer播放網絡音頻,介紹了MediaPlayer關於網絡音頻的緩沖和進度條控制的方法,這次再講解MediaPlayer播放網絡視頻。播放網絡視頻比播放網絡音頻多需要一個SurfaceView而已,已經熟悉MediaPlayer播放網絡音頻之後,相信大家對播放網絡視頻也能很快地掌握。先來看看本文程序運行截圖:

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

  本文程序的視頻來自http://daily3gp.com,大家可以替換程序中的視頻鏈接,試試其他影片。

  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.         <surfaceview android:id="@+id/surfaceView1"   
  4.                 android:layout_height="fill_parent" android:layout_width="fill_parent">  
  5.         <linearlayout android:layout_height="wrap_content"   
  6.                 android:layout_width="fill_parent" android:layout_gravity="bottom"  
  7.                 android:orientation="vertical">  
  8.                 <linearlayout android:orientation="horizontal"   
  9.                         android:layout_gravity="center_horizontal" android:layout_marginTop="4.0dip"  
  10.                         android:layout_height="wrap_content" android:layout_width="wrap_content">  
  11.                         <button android:layout_width="wrap_content"   
  12.                                 android:layout_height="wrap_content" android:id="@+id/btnPlayUrl"  
  13.                                 android:text="播放網絡視頻">  
  14.                         <button android:layout_height="wrap_content" android:id="@+id/btnPause"   
  15.                                 android:text="暫停" android:layout_width="80dip">  
  16.                         <button android:layout_height="wrap_content"   
  17.                                 android:layout_width="80dip" android:text="停止" android:id="@+id/btnStop">  
  18.                   
  19.                 <linearlayout android:orientation="horizontal"   
  20.                         android:layout_width="fill_parent" android:layout_height="wrap_content"  
  21.                         android:layout_marginBottom="20dip">  
  22.                         <seekbar android:paddingright="10dip" android:layout_gravity="center_vertical"   
  23.                                 android:paddingLeft="10dip" android:layout_weight="1.0"  
  24.                                 android:layout_height="wrap_content" android:layout_width="wrap_content"  
  25.                                 android:id="@+id/skbProgress" android:max="100">  

  Player.java是本文的核心,Player.java實現了“進度條更新”、“數據緩沖”、“SurfaceHolder生命周期”等功能,其中“SurfaceHolder生命周期”是視頻與音頻播放的最大區別,通過surfaceCreated()、surfaceDestroyed()、surfaceChanged()可以創建/釋放某些資源。下面這個地方需要注意一下:

Java代碼
  1. videoWidth = mediaPlayer.getVideoWidth();  
  2.                 videoHeight = mediaPlayer.getVideoHeight();  
  3.                 if (videoHeight != 0 && videoWidth != 0) {  
  4.                         arg0.start();  
  5.                 }  

  有些視頻是android播放器不能播放的,不能播放時videoHeight=0,videoWidth=0,以此來判斷是否播放視頻。Player.java源碼如下:

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

  test_videoplayer.java是主程序,負責調用Player類,其中關鍵部分是SeekBarChangeEvent這個SeekBar拖動的事件:SeekBar的Progress是0~SeekBar.getMax()之內的數,而MediaPlayer.seekTo()的參數是0~MediaPlayer.getDuration()之內數,所以MediaPlayer.seekTo()的參數是(progress/seekBar.getMax())*MediaPlayer.getDuration()。test_videoplayer.java源碼如下:

Java代碼
  1. package com.videoplayer;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.pm.ActivityInfo;  
  5. import android.net.Uri;  
  6. import android.os.Bundle;  
  7. import android.util.Log;  
  8. import android.view.SurfaceView;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12. import android.widget.SeekBar;  
  13.   
  14. public class test_videoplayer extends Activity {  
  15.         private SurfaceView surfaceView;  
  16.         private Button btnPause, btnPlayUrl, btnStop;  
  17.         private SeekBar skbProgress;  
  18.         private Player player;  
  19.   
  20.         /** Called when the activity is first created. */  
  21.         @Override  
  22.         public void onCreate(Bundle savedInstanceState) {  
  23.                 super.onCreate(savedInstanceState);  
  24.                 setContentView(R.layout.main);  
  25.                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
  26.                 surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView1);  
  27.   
  28.                 btnPlayUrl = (Button) this.findViewById(R.id.btnPlayUrl);  
  29.                 btnPlayUrl.setOnClickListener(new ClickEvent());  
  30.   
  31.                 btnPause = (Button) this.findViewById(R.id.btnPause);  
  32.                 btnPause.setOnClickListener(new ClickEvent());  
  33.   
  34.                 btnStop = (Button) this.findViewById(R.id.btnStop);  
  35.                 btnStop.setOnClickListener(new ClickEvent());  
  36.   
  37.                 skbProgress = (SeekBar) this.findViewById(R.id.skbProgress);  
  38.                 skbProgress.setOnSeekBarChangeListener(new SeekBarChangeEvent());  
  39.                 player = new Player(surfaceView, skbProgress);  
  40.   
  41.         }  
  42.   
  43.         class ClickEvent implements OnClickListener {  
  44.   
  45.                 @Override  
  46.                 public void onClick(View arg0) {  
  47.                         if (arg0 == btnPause) {  
  48.                                 player.pause();  
  49.                         } else if (arg0 == btnPlayUrl) {  
  50.                                 String url="http://daily3gp.com/vids/family_guy_penis_car.3gp";  
  51.                                 player.playUrl(url);  
  52.                         } else if (arg0 == btnStop) {  
  53.                                 player.stop();  
  54.                         }  
  55.   
  56.                 }  
  57.         }  
  58.   
  59.         class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {  
  60.                 int progress;  
  61.   
  62.                 @Override  
  63.                 public void onProgressChanged(SeekBar seekBar, int progress,  
  64.                                 boolean fromUser) {  
  65.                         // 原本是(progress/seekBar.getMax())*player.mediaPlayer.getDuration()  
  66.                         this.progress = progress * player.mediaPlayer.getDuration()  
  67.                                         / seekBar.getMax();  
  68.                 }  
  69.   
  70.                 @Override  
  71.                 public void onStartTrackingTouch(SeekBar seekBar) {  
  72.   
  73.                 }  
  74.   
  75.                 @Override  
  76.                 public void onStopTrackingTouch(SeekBar seekBar) {  
  77.                         // seekTo()的參數是相對與影片時間的數字,而不是與seekBar.getMax()相對的數字  
  78.                         player.mediaPlayer.seekTo(progress);  
  79.                 }  
  80.         }  
  81.   
  82. }  
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved