Skip to content

Commit 89b5420

Browse files
committed
Show notification even in STATE_IDLE
Currently the notification disappears immediately when the player enters an error or stopped state, but still has its media and could resume on user request. This can be fixed by only checking the existence of media and not the state when deciding to show a notification. PiperOrigin-RevId: 726901050 (cherry picked from commit f0da364)
1 parent d14807c commit 89b5420

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

‎RELEASENOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
* Muxers:
3636
* IMA extension:
3737
* Session:
38+
* Keep notification visible when playback enters an error or stopped
39+
state. The notification is only removed if the playlist is cleared or
40+
the player is released.
3841
* UI:
3942
* Downloads:
4043
* OkHttp Extension:

‎libraries/session/src/main/java/androidx/media3/session/MediaNotificationManager.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,7 @@ private void maybeStopForegroundService(boolean removeNotifications) {
251251

252252
private boolean shouldShowNotification(MediaSession session) {
253253
MediaController controller = getConnectedControllerForSession(session);
254-
return controller != null
255-
&& !controller.getCurrentTimeline().isEmpty()
256-
&& controller.getPlaybackState() != Player.STATE_IDLE;
254+
return controller != null && !controller.getCurrentTimeline().isEmpty();
257255
}
258256

259257
@Nullable

‎libraries/session/src/test/java/androidx/media3/session/MediaSessionServiceTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,55 @@ public void setUp() {
6767
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
6868
}
6969

70+
@Test
71+
public void service_sessionIdleNoMedia_createsNoNotification() {
72+
ExoPlayer player = new TestExoPlayerBuilder(context).build();
73+
MediaSession session = new MediaSession.Builder(context, player).build();
74+
ServiceController<TestService> serviceController = Robolectric.buildService(TestService.class);
75+
TestService service = serviceController.create().get();
76+
service.setMediaNotificationProvider(
77+
new DefaultMediaNotificationProvider(
78+
service,
79+
/* notificationIdProvider= */ unused -> 2000,
80+
DefaultMediaNotificationProvider.DEFAULT_CHANNEL_ID,
81+
DefaultMediaNotificationProvider.DEFAULT_CHANNEL_NAME_RESOURCE_ID));
82+
service.addSession(session);
83+
84+
// Give the service a chance to create a notification.
85+
ShadowLooper.idleMainLooper();
86+
87+
assertThat(getStatusBarNotification(2000)).isNull();
88+
89+
session.release();
90+
player.release();
91+
serviceController.destroy();
92+
}
93+
94+
@Test
95+
public void service_sessionIdleWithMedia_createsNotification() {
96+
ExoPlayer player = new TestExoPlayerBuilder(context).build();
97+
MediaSession session = new MediaSession.Builder(context, player).build();
98+
ServiceController<TestService> serviceController = Robolectric.buildService(TestService.class);
99+
TestService service = serviceController.create().get();
100+
service.setMediaNotificationProvider(
101+
new DefaultMediaNotificationProvider(
102+
service,
103+
/* notificationIdProvider= */ unused -> 2000,
104+
DefaultMediaNotificationProvider.DEFAULT_CHANNEL_ID,
105+
DefaultMediaNotificationProvider.DEFAULT_CHANNEL_NAME_RESOURCE_ID));
106+
service.addSession(session);
107+
108+
// Add media and give the service a chance to create a notification.
109+
player.setMediaItem(MediaItem.fromUri("asset:///media/mp4/sample.mp4"));
110+
ShadowLooper.idleMainLooper();
111+
112+
assertThat(getStatusBarNotification(2000)).isNotNull();
113+
114+
session.release();
115+
player.release();
116+
serviceController.destroy();
117+
}
118+
70119
@Test
71120
public void service_multipleSessionsOnMainThread_createsNotificationForEachSession() {
72121
ExoPlayer player1 = new TestExoPlayerBuilder(context).build();

0 commit comments

Comments
 (0)