Disable Android's VideoView requestAudioFocus when playing a video?

前端 未结 5 2127
野的像风
野的像风 2021-02-13 06:04

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 paus

5条回答
  •  闹比i
    闹比i (楼主)
    2021-02-13 06:46

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

提交回复
热议问题