Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exoplayer setPlayWhenReady() not working why?

I use this ExoPlayer demo for playing audio streaming. I modified code for playing audio with notification. for this I put Exoplayer code in service class, but pause method not working don't know what happens but setPlayWhenReady(false) not working in service. Check this modified source code

like image 757
P A Gosai Avatar asked Sep 05 '25 10:09

P A Gosai


1 Answers

That's because every time you setPlayWhenReady(false) , the onPlayerStateChanged method of ExoPlayer Listener Called and you are setting setPlayWhenReady(true) in the listener. So that's what you need to do : 1. define a field in your service :

boolean isPreparing;

2. set isPreparing = true before initializing the ExoPlayer :

isPreparing = true;
player.prepare(...);

3. in the onPlayerStateChanged method of ExoPlayer Listener :

@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
  if(isPreparing && playbackState == ExoPlayer.STATE_READY){
     // do whatever you want
     isPreparing = false;
  }
}

And that's it.

like image 62
ParSa Avatar answered Sep 07 '25 23:09

ParSa