Skip to content

Commit

Permalink
Only COMMAND_PLAY starts the service in the foreground
Browse files Browse the repository at this point in the history
This change makes all notification actions start MediaSessionService
in the background except COMMAND_PLAY which starts the service
in the foreground. This is to avoid ANRs that are raised if we don't
call MediaSessionService.startForeground() within 5 seconds since the
service was started in the foreground.

We only call MediaSessionService.startForeground() when
Player.getPlayWhenReady() returns true, and only COMMAND_PLAY sets
playWhenReady to true.

Issue: #20

#minor-release

PiperOrigin-RevId: 433229604
  • Loading branch information
christosts authored and icbaker committed Mar 9, 2022
1 parent 2f4630a commit b94ca4f
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public PendingIntent createMediaActionPendingIntent(@Command long command) {
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.setComponent(new ComponentName(service, service.getClass()));
intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
if (Util.SDK_INT >= 26 && command != COMMAND_PAUSE && command != COMMAND_STOP) {
return Api26.createPendingIntent(service, keyCode, intent);
if (Util.SDK_INT >= 26 && command == COMMAND_PLAY) {
return Api26.createForegroundServicePendingIntent(service, keyCode, intent);
} else {
return PendingIntent.getService(
service,
Expand Down Expand Up @@ -137,7 +137,8 @@ public Bundle getCustomActionExtras(Intent intent) {
private static final class Api26 {
private Api26() {}

public static PendingIntent createPendingIntent(Service service, int keyCode, Intent intent) {
public static PendingIntent createForegroundServicePendingIntent(
Service service, int keyCode, Intent intent) {
return PendingIntent.getForegroundService(
service, /* requestCode= */ keyCode, intent, PendingIntent.FLAG_IMMUTABLE);
}
Expand Down

0 comments on commit b94ca4f

Please sign in to comment.