I have a Fragment which contains an instance of Exoplayer which I provide with Dagger 2 like this:
@Inject SimpleExoPlayer mPlayer;
For the most part, this works well, I can play stuff on my player, the state is correctly restored on lifecycle changes, etc.
The issue I have is that when I click on the Overview button, navigate to another app, and then back, the player is in a black screen state where the seek bar has been restored to its saved position, but it will not play anything, nor respond to play commands on the UI, and just stays black until the Fragment gets recreated.
I am now trying to modify things to get my code to deal with the player just like in this code lab:
private void releasePlayer() {
if (player != null) {
playbackPosition = player.getCurrentPosition();
currentWindow = player.getCurrentindowIndex();
playWhenReady = player.getPlayWhenReady();
player.release();
player = null;
}
}
which should be called in onStop()
above API 23.
The initializePlayer()
is called in onStart()
and it is supposed to look like this. In my case the only difference is that I don't create the player, Dagger does it for me on creation.
private void initializePlayer() {
player = ExoPlayerFactory.newSimpleInstance(
new DefaultRenderersFactory(this),
new DefaultTrackSelector(), new DefaultLoadControl());
playerView.setPlayer(player);
player.setPlayWhenReady(playWhenReady);
player.seekTo(currentWindow, playbackPosition);
[...]
}
Note that both code snippets are from the linked code lab.
This is where I am struggling with Dagger. When the Fragment is created or recreated, everything is fine, as Dagger will provide me with a player, but if I have set the player to null
in onStop()
, and the Fragment is not destroyed and it is restored and only onStart() and onResume() are called, Dagger will not create a new instance, unless I am missing a way on how to this.
Can anyone point to some code examples of Dagger2 and ExoPlayer set up, preferably with a Fragment? Also shed some light into this, please :)
After the hint on using a Provider<> I solved the issue by having a:
@Inject Provider<SimpleExoPlayer> mSimpleExoPlayerProvider
And:
SimpleExoPlayer mPlayer
So when I initialize the player inside onStart
I will get a new instance of the Player from the Provider if there is no instance attached to mPlayer, and hold to that player instance util I release the player in onStop
where I set mPlayer to null.
if (mPlayer == null) {
// Get new instance of player if mPlayer is null
mPlayer = mSimpleExoPlayerProvider.get();
}
Dagger will not create a new instance, unless I am missing a way on how to this.
If all you want to do is get a new instance from Dagger every time, then you should inject Provider<SimpleExoPlayer>
and call provider.get()
when you need a new object.
For this to work the player needs to be unscoped, or you will get the same object every time.