Prasanna Kumar
Prasanna Kumar

Reputation: 368

Exoplayer - Plays same audio over and over on reopening the activity

I'm creating an exoplayer instance and adding a stream URL as progressive media source, preparing the player and playing the audio. When I go back to previous activity and open the player activity again, another instance of player is running the same audio (Two instances playing same audio simultaneously). Also I have a mute button. It works as expected when I open the activity for the first time. On reopening the activity, the mute button mutes only the ExoPlayer instance of the current activity

I tried Moving the ExoPlayer code to another class and calling them with public functions, but didn't work

val dataSourceFactory = DefaultDataSourceFactory(this, Util.getUserAgent(this, packageName))
val newMediaSource = ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse("STREAM URL"));
val exoPlayer = ExoPlayerFactory.newSimpleInstance(this);
exoPlayer.prepare((newMediaSource));
exoPlayer.playWhenReady = true;

muteButton.setOnClickListener {
    if(muteButton.tag == "muted")
    {
        exoPlayer.volume = 1f;
        muteButton.tag = "unmuted";
    }
    else
    {
        exoPlayer.volume = 0f;
        muteButton.tag = "muted";
    }
}

I want the same ExoPlayer to run on reopening the activity and also mute that particular instance

Upvotes: 1

Views: 769

Answers (1)

Prasanna Kumar
Prasanna Kumar

Reputation: 368

I ended up using shared preference to save the service's state. So whenever the service is started, the boolean in shared preference is set to true. So I can start the service only when the boolean is false. Also I am using Broadcast receiver in both activity and service to exchange messages.

Upvotes: 1

Related Questions