1

I'm trying to assign my PlayerView to the SimpleExoPlayer that is initialized in my foreground service. I tried using onBind(), but whenever I bind the context to the service the SimpleExoPlayer is no longer able to play when I exit the app (I believe this is because when I bind the service to my context it stops running on its own process and starts running on the main application process). I was wondering if there is a know solution for synchronizing a PlayerView to a player already running in the foreground?

My Service onStartCommand():

@Override
public int onStartCommand(@NotNull Intent intent, int flags, int startId){
  final Context context = this;
  DefaultTrackSelector trackSelector = new DefaultTrackSelector(context);
  trackSelector.setParameters(trackSelector.buildUponParameters().setMaxVideoSizeSd());
  player = new SimpleExoPlayer.Builder(context).setTrackSelector(trackSelector).build();
  player.setPlayWhenReady(playWhenReady);
  player.seekTo(currentWindow, playbackPosition);
  player.prepare();
  playerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
    context, PLAY_MUSIC_CHANNEL_ID, R.string.play_music_channel_name,
    PLAY_MUSIC_NOTIFICATION_ID,
    new PlayerNotificationManager.MediaDescriptionAdapter(){/*omitted for simplifying issue, but 
    everything is working*/},
    new PlayerNotificationManager.NotificationListener(){
      @override
      public void onNotificationPosted(int notificationId, Notification notification, boolean ongoing) {
        startForeground(notificationId, notification);
      }
      @Override
      public void onNotificationCancelled(int notificationId, boolean dismissedByUser) {
        stopSelf();
      }
    }
  );
  return START_STICKY;
}

My Service onBind():

private final IBinder binder = new LocalBinder();

public class LocalBinder extends Binder {
  public AudioPlayerService getService(){
    return AudioPlayerService.this;
  }
}

@nullable
@override
public IBinder onBind(Intent intent) {
  return binder;
}

I start my service in a different class like this:

Intent intent = new Intent(this, AudioPlayerService.class);
Util.startForegroundService(getApplicationContext(), intent);

My bind() methods in the music player fragment for attaching foreground player to Player View:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(getContext(), AudioPlayerService.class);
        getContext().bindService(intent, musicPlayerConnection, Context.BIND_AUTO_CREATE);
      }
    }


private AudioPlayerService audioPlayerService;
private boolean isBound;
    public ServiceConnection musicPlayerConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            AudioPlayerService.LocalBinder binder = (AudioPlayerService.LocalBinder) service;
            audioPlayerService = binder.getService();
            isBound = true;
            initializePlayer();
            Log.d("ServiceConnection","connected");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isBound = false;
            Log.d("ServiceConnection","disconnected");
        }
    };

    private void initializePlayer() {
        if (player == null && isBound) {
          player = audioPlayerService.getPlayer();
        }
        musicPlayerView.setPlayer(player);
        musicPlayerView.setShowShuffleButton(true);
    }

0 Answers0