将前台服务与 PlayerView UI 同步?

问题描述

我正在尝试将我的 PlayerView 分配给在我的前台服务中初始化的 SimpleExoPlayer。我尝试使用 onBind(),但是每当我将上下文绑定到服务时,当我退出应用程序时,SimpleExoPlayer 就不再能够播放(我相信这是因为当我将服务绑定到我的上下文时,它会停止在自己的进程上运行并开始在主应用程序进程上运行)。我想知道是否有将 PlayerView 同步到已经在前台运行的播放器的已知解决方案?

我的服务 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;
}

我的服务 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;
}

我在一个不同的类中开始我的服务,如下所示:

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

音乐播放器片段中用于将前台播放器附加到播放器视图的我的 bind() 方法

    @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);
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)