播放视频时禁用Android的VideoView requestAudioFocus?

编程入门 行业动态 更新时间:2024-10-10 01:18:13
本文介绍了播放视频时禁用Android的VideoView requestAudioFocus?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在构建一个记录和播放视频的应用程序.我希望这样做不会影响背景音乐的播放,即,如果我开始播放视频,我不想暂停其他应用程序的音频.但是,在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?

本文发布于:2023-11-27 18:51:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639196.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视频   Android   requestAudioFocus   VideoView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!