出自: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