Search code examples
androidandroid-mediaplayerandroid-videoviewandroid-audiomanager

How to setAudioSessionId, setAudioAttributes, and setScreenOnWhilePlaying in VideoView


Sorry, not sure if noob question. As the title says, I want to use setAudioSessionId, setAudioAttributes, and setScreenOnWhilePlaying in a VideoView. But I can't seem to find anything that teaches how to do it. There's no getMediaPlayer method in VideoView and setting them in onPrepared where the MediaPlayer instance is getting passed to seems to be too late. So how can I use them in VideoView, is there some other VideoView initialization callback that I can use? Or is there anything that would allow me to set the stream type for the app in general? And maintain screen on without using wakelock like setScreenOnWhilePlaying?

Thank you very much! :)


Solution

  • VideoView is a wrapper of MediaPlayer. VideoView instantiate a MediaPlayer object inside of it and never exposes it.

    • Regarding setAudioAttributes(), VideoView provides the same API and you can use it.
    • MediaPlayer.setScreenOnWhilePlaying(true) is called within VideoView. So you don't need to worry about calling setScreenOnWhilePlaying on your side.
    • For setAudioSessionId(), VideoView doesn't provide a corresponding API to it, but it has getAudioSessionId(). Won't it satisfy your needs?