Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Android's VideoView requestAudioFocus when playing a video?

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?

like image 581
diana Avatar asked Jan 23 '15 22:01

diana


People also ask

What is VideoView in Android?

android.widget.VideoView. Displays a video file. The VideoView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting.


1 Answers

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)
}

For older version, there's a workaround described here: https://stackoverflow.com/a/31569930/993439

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);
like image 137
longi Avatar answered Sep 29 '22 13:09

longi