androidandroid-picture-in-pictureandroid-13

Android 13 - Picture-in-picture mode - change action icon


I have an activity player which is streaming a content, this activity can go into picture-in-picture mode, everything is working fine as expected so far, but on Android 13 the play/pause button icon is not getting updated.

I will post my implementation below,

public static PictureInPictureParams buildPIPParams(Context context, boolean nowPlaying, boolean isLive) {
    Icon icon;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
       if (nowPlaying) {
          icon = Icon.createWithResource(context, R.drawable.pause_livetv_ico);
       } else {
          icon = Icon.createWithResource(context, R.drawable.play_livetv_ico);
       }
       PendingIntent broadcast = PendingIntent.getBroadcast(context, 0,
                    new Intent("PIP_PLAY_PAUSE_PLAYER"), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

       RemoteAction remoteAction = null;

       remoteAction = new RemoteAction(icon, "", "", broadcast);

       List<RemoteAction> actions = new ArrayList<>();
       actions.add(remoteAction);

       Rational aspectRatio = new Rational(16, 9);
       PictureInPictureParams build = new PictureInPictureParams.Builder()
              .setAspectRatio(aspectRatio)
              .setActions(actions)
              .build();

       return build;
    }
}

in the above function I'm building the PictureInPictureParams and passing it to enterPictureInPictureMode function to set the initial icon when the user launch PiP mode and this is working fine even for android 13.

then when the user press on the play/pause button inside the PiP mode the broadcast is being fired and I'm pausing/resuming the stream (this is working fine as-well), then I'm trying to update the icon of the action to be pause/resume icon by calling same above function and passing the PictureInPictureParams to the setPictureInPictureParams method, this is working fine on android 12 and below but on android 13 it's not working

implementation:

@Override
public void onReceive(Context context, Intent intent) { // on receive PIP_PLAY_PAUSE_PLAYER broadcast event
      onPlayButtonClickedWithEvent(false); // this is working fine the stream is being paused/resumed
      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
          PictureInPictureParams pictureInPictureParams = RTVUtils.buildPIPParams(LiveTVActivity.this, nowPlaying, true);
          if (pictureInPictureParams != null)
              setPictureInPictureParams(pictureInPictureParams); // this is not working on android 13 devices 
          }
       }
}

anyone faced such problem? is it from android 13 OS itself? google didn't mention any update regarding this in their release notes for android 13.


Solution

  • The solution is very simple.

    Edit the following line:-

     PendingIntent broadcast = PendingIntent.getBroadcast(context, 0,
                    new Intent("PIP_PLAY_PAUSE_PLAYER"), 
    PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    

    and replace it with:-

     PendingIntent broadcast = PendingIntent.getBroadcast(context, nowPlaying?0:1,
                    new Intent("PIP_PLAY_PAUSE_PLAYER"), 
    PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    

    This is because since android 13, remote actions require distinct request codes for showing different icons.