3

I use ExoPlayer with standard PlayerNotificationManager class from library.

When I close my app that playing music with swipe, Android fully kill it, music stopped, notification continues to be displayed, but buttons isn't active. Can I implement playing foreground without using Android Service? And if yes, how to do it? Maybe ExoPlayer have standard Service that allows to implement it?

Player initialization:

SimpleExoPlayer exoPlayer = new SimpleExoPlayer.Builder(context, new DefaultRenderersFactory(context))
                .setTrackSelector(new DefaultTrackSelector(context))
                .build();
exoPlayer.setAudioAttributes(audioAttributes);
exoPlayer.setHandleAudioBecomingNoisy(true);

MediaSessionCompat mediaSession = new MediaSessionCompat(context, "Newtone");
MediaSessionConnector mediaSessionConnector = new MediaSessionConnector(mediaSession);
mediaSessionConnector.setPlayer(exoPlayer);

PlayerNotificationManager notificationManager = PlayerNotificationManager
            .createWithNotificationChannel(
                    context, NOTIFICATION_CHANNEL_ID, R.string.app_name, R.string.app_name, NOTIFICATION_ID,
                    mediaDescriptionAdapter, notificationListener);

notificationManager.setPlayer(exoPlayer);
notificationManager.setMediaSessionToken(mediaSession.getSessionToken());
notificationManager.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
notificationManager.setUseNavigationActionsInCompactView(true);
notificationManager.setPriority(NotificationCompat.PRIORITY_MAX);
notificationManager.setUseChronometer(false);
notificationManager.setFastForwardIncrementMs(0);
notificationManager.setRewindIncrementMs(0);
  • If the music keeps playing, then the service is still alive. Post some code with your player and notification manager initialization, please. – Giorgos Neokleous Jan 24 '20 at 09:16
  • @GiorgosNeokleous, I added Player initialization and no, music not keeps playing after I swipe application – Арслан Халиков Jan 24 '20 at 10:11
  • Does the initialization happen in the service or in an activity or fragment? All the player and notification manager should be setup and live in the service and not the activity that could be killed. – Giorgos Neokleous Jan 24 '20 at 10:33
  • @GiorgosNeokleous it implemented in separate class, instance of which is provided by dagger2 that initialize in MainApplication.onCreate(). I thought that if instance of class is create in MainApplication, then it will live longer than activity – Арслан Халиков Jan 24 '20 at 10:53
  • The problem with initalization in Service is that I need an instance of SimpleExoPlayer and PlayerNotificationManager not only in Service – Арслан Халиков Jan 24 '20 at 10:56

0 Answers0