Disable Android's VideoView requestAudioFocus when playing a video?

前端 未结 5 2136
野的像风
野的像风 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条回答
  •  青春惊慌失措
    2021-02-13 06:37

    use audioManager.abandonAudioFocus(null)

    If you look at the VideoView code you will notice it calls the method audioManager.requestAudioFocus with null for the OnAudioFocusChangeListener. When you register a listener with the AudioManager it uses this method to make an ID for the listener

    private String getIdForAudioFocusListener(OnAudioFocusChangeListener l) {
        if (l == null) {
            return new String(this.toString());
        } else {
            return new String(this.toString() + l.toString());
        }
    }
    

    which generates the same ID every time you use null. So if you call abandonAudioFocus with null it will remove any listener that was added with null as the parameter for the OnAudioFocusChangeListener

提交回复
热议问题