问题现象:使用VideoView写了一个悬浮框播放视频程序,在悬浮框代码中注册音频焦点监听事件,监听悬浮框的音频焦点获取和丢失。发现悬浮框和Music同时播放了。
分析:通过log分析当时音频焦点的获取情况,如下:
I/MediaFocusControl( 652): AudioFocus requestAudioFocus() from android.media.AudioManager@2109af33com.android.gallery3d.app.MoviePlayer$42@38f352f0
I/MediaFocusControl( 652): AudioFocus requestAudioFocus() from android.media.AudioManager@347bf9ccom.android.gallery3d.ui.FloatVideoView$1@10964da5
I/MediaFocusControl( 652): AudioFocus abandonAudioFocus() from android.media.AudioManager@2109af33com.android.gallery3d.app.MoviePlayer$42@38f352f0
I/MediaFocusControl( 652): AudioFocus removeFocusStackEntry(): removing entry for android.media.AudioManager@2109af33com.android.gallery3d.app.MoviePlayer$42@38f352f0
I/MediaFocusControl( 652): AudioFocus requestAudioFocus() from android.media.AudioManager@347bf9c
I/MediaFocusControl( 652): AudioFocus abandonAudioFocus() from android.media.AudioManager@204847cc
I/MediaFocusControl( 652): AudioFocus requestAudioFocus() from android.media.AudioManager@c5fd86dcom.android.music.MediaPlaybackService$3@a0e8ca2
会发现,首先视频播放器MoviePlayer获取焦点。再打开悬浮框,悬浮框FloatVideoView获取焦点,MoviePlayer丢失焦点。可是在这里我们会发现,紧接着未知东西又申请了音频焦点,造成悬浮框FloatVideoView丢失音频焦点,而且没有再获取。故,当进入Music时候两者同时响起。
通过跟踪代码发现在VideoView中有申请音频焦点:
/frameworks/base/core/java/android/widget/VideoView.java
protected void openVideo() {
........<br />
AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);<br />
am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
........
}
同时,由于requestAudioFocus的监听事件(第一个参数)为空,所以在log中不会打印出申请音频焦点的类名。也就造成了该现象。
<p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>