播放视频时禁用Android的VideoView requestAudioFocus? [英] Disable Android's VideoView requestAudioFocus when playing a video?
问题描述
我正在构建一个记录和播放视频的应用程序.我希望这样做不会影响背景音乐的播放,即,如果我开始播放视频,我不想暂停其他应用程序的音频.但是,在Lollipop上,调用私有方法VideoView.openVideo()
时,Android的VideoView类会自动请求音频焦点:
I'm building an app that records and plays back video. I would like to do so without affecting background music playback, i.e. if I begin playing a video, I do not want to pause other apps' audio. However, on Lollipop, Android's VideoView class automatically requests audio focus when the private method VideoView.openVideo()
is called:
AudioManager am = (AudioManager) super.getSystemService(name);
am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
有关如何解决此问题的任何建议?
Any suggestions on how to get around this?
推荐答案
从Android SDK 26开始,您可能要使用VideoView
和
Starting with Android SDK 26 you may want to use VideoView
and
if(Build.VERSION.SDK_INT >= Build.Version_CODES.O){
//set this BEFORE start playback
videoView.setAudioFocusRequest(AudioManager.AUDIOFOCUS_NONE)
}
对于较旧的版本,此处描述了一种解决方法: https://stackoverflow.com/a/31569930/993439
For older version, there's a workaround described here: https://stackoverflow.com/a/31569930/993439
基本上,复制VideoView
的源代码并取消注释以下行
Basically, copy source code of VideoView
and uncomment following lines
AudioManager am = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
这篇关于播放视频时禁用Android的VideoView requestAudioFocus?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!