安卓浏览器网页播放视频时后台声音不停止

出自:http://www.xdty.org/1548

浏览器在线播放优酷视频时没有调用系统的播放器,而是在网页内做了解析,这个处理在frameworks/base/core/java/android/webkit/HTML5VideoView.java中实现。通过调用AudioManager并设置音频焦点来实现后台声音的暂停和继续播放。

关键在于调用requestAudioFocus,如下所示:

mAudioManager  =  (AudioManager ) mProxy. getContext ( ). getSystemService ( Context. AUDIO_SERVICE ) ;
mAudioManager. requestAudioFocus ( null, AudioManager. STREAM_MUSIC,
AudioManager. AUDIOFOCUS_GAIN_TRANSIENT ) ;

在视频播放具体修改见如下代码:

diff  --git a /HTML5VideoView. java b /HTML5VideoView. java
index 0e8a5db..3aa36b4  100755
--- a /HTML5VideoView. java
+++ b /HTML5VideoView. java
@@  - 17, 6  + 17, 10 @@
  package  android.webkit ;
 
  import  android.media.MediaPlayer ;
+ // Begin: [tianyu, 2013-08-23] Add for "Stop other voice while play online video" @{
+ import  android.media.AudioManager ;
+ import  android.content.Context ;
+ // End: [tianyu, 2013-08-23] Add for "Stop other voice while play online video" @}
  import  android.net.Uri ;
  import  android.webkit.HTML5VideoViewProxy ;
  import  java.io.IOException ;
@@  - 62, 6  + 66, 9 @@  public  class HTML5VideoView  implements MediaPlayer. OnPreparedListener  {
      // player at the same time. We may recreate a new one and abandon the old
      // one at transition time.
      protected  static MediaPlayer mPlayer  =  null ;
+     // Begin: [tianyu, 2013-08-23] Add for "Stop other voice while play online video" @{
+     private  static AudioManager mAudioManager  =  null ;
+     // End: [tianyu, 2013-08-23] Add for "Stop other voice while play online video" @}
      protected  static  int mCurrentState  =  - 1 ;
 
      // We need to save such info.
@@  - 89, 6  + 96, 11 @@  public  class HTML5VideoView  implements MediaPlayer. OnPreparedListener  {
                 mTimer. schedule ( new TimeupdateTask (mProxy ), TIMEUPDATE_PERIOD,
                         TIMEUPDATE_PERIOD ) ;
              }
+             // Begin: [tianyu, 2013-08-23] Add for "Stop other voice while play online video" @{
+            mAudioManager  =  (AudioManager ) mProxy. getContext ( ). getSystemService ( Context. AUDIO_SERVICE ) ;
+            mAudioManager. requestAudioFocus ( null, AudioManager. STREAM_MUSIC,
+            AudioManager. AUDIOFOCUS_GAIN_TRANSIENT ) ;
+             // End: [tianyu, 2013-08-23] Add for "Stop other voice while play online video" @}
             mPlayer. start ( ) ;
             setPlayerBuffering ( false ) ;
          }
@@  - 97, 6  + 109, 11 @@  public  class HTML5VideoView  implements MediaPlayer. OnPreparedListener  {
      public  void pause ( )  {
          if  (isPlaying ( ) )  {
             mPlayer. pause ( ) ;
+             // Begin: [tianyu, 2013-08-23] Add for "Stop other voice while play online video" @{
+             if  (mAudioManager  !=  null )  {
+                mAudioManager. abandonAudioFocus ( null ) ;
+             }
+             // End: [tianyu, 2013-08-23] Add for "Stop other voice while play online video" @}
          }  else  if  (mCurrentState  == STATE_PREPARING )  {
             mPauseDuringPreparing  =  true ;
          }
@@  - 155, 6  + 172, 11 @@  public  class HTML5VideoView  implements MediaPlayer. OnPreparedListener  {
          if  (mPlayer  !=  null  && mCurrentState  != STATE_RELEASED )  {
             mPlayer. release ( ) ;
             mPlayer  =  null ;
+             // Begin: [tianyu, 2013-08-23] Add for "Stop other voice while play online video" @{
+             if  (mAudioManager  !=  null )  {
+                mAudioManager. abandonAudioFocus ( null ) ;
+             }
+             // End: [tianyu, 2013-08-23] Add for "Stop other voice while play online video" @}
          }
         mCurrentState  = STATE_RELEASED ;
      }

附件:

HTML5VideoView


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值