I implemented a test app using SimpleExoPlayer which works fine. I would like now to add audiofocus to my app.
To start playing with audio focus when the user launches the app is easy. Adding the usual code at the end of onCreate() :
if (AudioManager.AUDIOFOCUS_REQUEST_GRANTED == audioManager.requestAudioFocus(...
works fine.
But I have problem to request the audiofocus when the user presses the play button, which is encapsulated by the PlaybackControlView I recall.
Is other words, how can I capture the play button click event when using SimpleExoPlayer and SimpleExoPlayerView (PlaybackControlView)?
Redifining simpleExoPlayer.setPlayWhenReady() does not seem possible because the instance of SimpleExoPlayer is given by a Factory, not from instantation with new keyword.
simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(...)
Any other ideas than solving this problem using the play button? Thanks in advance.
UPDATE (17/02/17) :
This issue on github.com/google/ExoPlayer is labelled as ENHANCEMENT. here
They first answer was :
You can do this by adding a listener to the ExoPlayer instance (ExoPlayer.addListener) and implementing the listener's onPlayerStateChanged to obtain or release audio focus as necessary. Note that onPlayerStateChanged will be called whenever playWhenReady is changed.
The small problem with this solution is that onPlayerStateChanged() is called after the audio/video starts playing, not before. I implemented it and the very short overlapping when both audios play at the same time is not noticeable from the ear of course. So it could be considered as a good solution if you need. I'll let you know when/if the ExoPlayer team comes with a better solution.
ExoPlayer design team fixed the issue in the patch ref'd below :