Exoplayer: How to remove PlayerNotificationManager when playlist arrive some track ?

Created on 24 Apr 2019  路  3Comments  路  Source: google/ExoPlayer

I have some mediasource in ConcatenatingMediaSource that are non playable and I update my Player view to show a button. I want to remove notification of my service for these item. I detect these non playable mediasource in the onPlayerError listener.

```@Override
public void onPlayerError(ExoPlaybackException error) {

        switch (error.type) {
            case ExoPlaybackException.TYPE_SOURCE:
                String myError=error.getSourceException().getMessage();
                if (myError.contains("Response code: 404") ||
                        myError.contains("pdf") ||
                        myError.contains("None of the available extractors (MatroskaExtractor, FragmentedMp4Extractor, Mp4Extractor, Mp3Extractor, AdtsExtractor, Ac3Extractor, TsExtractor, FlvExtractor, OggExtractor, PsExtractor, WavExtractor, AmrExtractor) could read the stream.")) {
                    isDocument = true;
                    Intent intent=new Intent(BaseExoWithServiceActivity.this,PlayerService.class);
                    intent.putExtra(ACTION_SHOW_NOTIFICATION,2);
                    startService(intent);
                    updateButtonVisibilities(true);
                    return;
                } 
        }
    }
And in the PlayerService I write it :

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
int action = intent.getIntExtra(ACTION_SHOW_NOTIFICATION, -1);
switch (action) {
case 2:
Log.e(TAG,"stop case");
stopForeground(true);
stopSelf();
break;
default:
Log.e(TAG, "default");
break;

        }
    }
    return START_NOT_STICKY;
}

```
But in some times this solution didn't work.

question

Most helpful comment

You are saying it does sometimes not work. Do you mean that the notification is not always removed? I actually think calling stopForeground(true) seems about right to remove the notification.

To make the PlayerNotificationManager remove the notification, you can in case 2) call playerNotificationManager.setPlayer(null) which will make the manager remove the notification and call NotificationListener.onNotificationCancelled.

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            int action = intent.getIntExtra(ACTION_SHOW_NOTIFICATION, -1);
            switch (action) {
                case 2:
                    Log.e(TAG,"stop case");
                    playerNotificationManager.setPlayer(null);
                    break;
                default:
                    Log.e(TAG, "default");
                    break;

            }
        }
        return START_NOT_STICKY;
    }

new PlayerNotificationManager.NotificationListener() {
  @Override
  public void onNotificationCancelled(int notificationId, boolean dismissedByUser) {
     stopForeground(false);
     stopSelf();
  }
};

All 3 comments

You are saying it does sometimes not work. Do you mean that the notification is not always removed? I actually think calling stopForeground(true) seems about right to remove the notification.

To make the PlayerNotificationManager remove the notification, you can in case 2) call playerNotificationManager.setPlayer(null) which will make the manager remove the notification and call NotificationListener.onNotificationCancelled.

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            int action = intent.getIntExtra(ACTION_SHOW_NOTIFICATION, -1);
            switch (action) {
                case 2:
                    Log.e(TAG,"stop case");
                    playerNotificationManager.setPlayer(null);
                    break;
                default:
                    Log.e(TAG, "default");
                    break;

            }
        }
        return START_NOT_STICKY;
    }

new PlayerNotificationManager.NotificationListener() {
  @Override
  public void onNotificationCancelled(int notificationId, boolean dismissedByUser) {
     stopForeground(false);
     stopSelf();
  }
};

Closing issue due to inactivity.

In addition to playerNotificationManager.setPlayer(null) in onStartCommand(...) the SimpleExoPlayer instance needs to be set to false player.playWhenReady = false in order to stop playing.

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    intent?.let {
        when (it.getStringExtra(PLAYER_KEY)) {
            PAUSE.name -> player.playWhenReady = false
            PLAY.name -> player.playWhenReady = true
            STOP.name -> {
                player.playWhenReady = false
                playerNotificationManager.setPlayer(null)
            }
        }
Was this page helpful?
0 / 5 - 0 ratings