我正在构建一个记录和播放视频的应用程序.我希望这样做不会影响背景音乐的播放,即,如果我开始播放视频,我不想暂停其他应用程序的音频.但是,在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) }对于较旧的版本,此处描述了一种解决方法: stackoverflow/a/31569930/993439
For older version, there's a workaround described here: stackoverflow/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?
发布评论