我有一个服务,我使用 ExoPlayer 在其中播放音乐。我正在为我的应用程序显示一个通知,因为它是一个前台服务,并且我不断更新 ExoPlayer 的 onPlayerStateChanged 回调方法中的通知:
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
...
showNotification(state, sample);
}
private void showNotification(PlaybackStateCompat state, Sample sample) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "Id");
int icon;
String play_pause;
if (state.getState() == PlaybackStateCompat.STATE_PLAYING) {
icon = R.drawable.exo_controls_pause;
play_pause = getString(R.string.pause);
} else {
icon = R.drawable.exo_controls_play;
play_pause = getString(R.string.play);
}
NotificationCompat.Action playPauseAction = new NotificationCompat.Action(
icon, play_pause,
MediaButtonReceiver.buildMediaButtonPendingIntent(this,
PlaybackStateCompat.ACTION_PLAY_PAUSE));
NotificationCompat.Action restartAction = new NotificationCompat
.Action(R.drawable.exo_controls_previous, getString(R.string.restart),
MediaButtonReceiver.buildMediaButtonPendingIntent
(this, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS));
NotificationCompat.Action nextAction = new NotificationCompat
.Action(R.drawable.exo_controls_next, getString(R.string.next),
MediaButtonReceiver.buildMediaButtonPendingIntent
(this, PlaybackStateCompat.ACTION_SKIP_TO_NEXT));
PendingIntent contentPendingIntent = PendingIntent.getActivity
(this, 0, new Intent(this, MainActivity.class), 0);
Intent intent = new Intent(this, StopServiceBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
builder.setContentTitle(sample.getTitle())
.setContentText(sample.getComposer())
.setContentIntent(contentPendingIntent)
.setDeleteIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_music_note)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.addAction(restartAction)
.addAction(playPauseAction)
.addAction(nextAction).setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setMediaSession(mMediaSession.getSessionToken())
.setShowActionsInCompactView(1, 2));
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "Id";
NotificationChannel channel = new NotificationChannel(
channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_LOW);
mNotificationManager.createNotificationChannel(channel);
builder.setChannelId(channelId);
}
Notification notificationCompat = builder.build();
if (state.getState() == PlaybackStateCompat.STATE_PAUSED) {
stopForeground(false);
mNotificationManager.notify(NOTIFICATION_ID, notificationCompat);
} else {
startForeground(NOTIFICATION_ID, notificationCompat);
}
}
PAUSE
正如您在 state 为I时在方法末尾看到的那样stopForeground
。这将在一分钟内销毁服务,因此通知将被取消,如以下线程中所述:https ://github.com/google/ExoPlayer/issues/5954
我是 SoundCloud 应用程序的用户。在他们的应用程序中,似乎当状态为 时PAUSE
,他们也stopForeground
因为通知是可关闭的。但是,如果我不手动关闭通知,通知将始终保留在状态栏中,并且似乎 Service 在他们的应用程序中没有被破坏。The scenario that I explained for SoundCloud is just a guess
你认为它是如何在他们的应用程序中实现的?
如果需要检查,可以在这里找到我的示例的完整源代码:https ://github.com/AliRezaeiii/ExoPlayerSample
/**
* Release ExoPlayer.
*/
private void releasePlayer() {
mExoPlayer.removeListener(this);
mExoPlayer.stop();
mExoPlayer.release();
mExoPlayer = null;
}
/**
* Release the player when the service is destroyed.
*/
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy()");
super.onDestroy();
releasePlayer();
mMediaSession.setActive(false);
}