上次讲解了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, 01000);  
  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. }  
本文发布:Android开发网
本文地址:http://www.jizhuomi.com/android/course/525.html
2016年1月11日
发布:鸡啄米 分类:Android开发教程 浏览: 评论:0