Service 将ExoPlayer实例从活动传递到绑定服务?

Service 将ExoPlayer实例从活动传递到绑定服务?,service,notifications,instance,bind,exoplayer,Service,Notifications,Instance,Bind,Exoplayer,我正在尝试使用ExoPlayer制作一个视频播放器,它也可以在后台工作,你可以通过通知来控制它。我已经为通知创建了ExoPlayer和前台服务,并绑定了它们。目前它按预期工作,唯一的问题是我不希望活动播放器在我关闭通知时停止工作。发生这种情况是因为我正在服务中创建ExoPlayer实例,然后将实例传递给活动,因此当我关闭通知时,实例将丢失。是否有一种方法可以初始化活动中的播放器实例,然后将该实例传递给服务,这样我仍然可以从通知中控制视频,而不会在通知关闭后冒丢失实例的风险 我对安卓相当陌生,这是

我正在尝试使用ExoPlayer制作一个视频播放器,它也可以在后台工作,你可以通过通知来控制它。我已经为通知创建了ExoPlayer和前台服务,并绑定了它们。目前它按预期工作,唯一的问题是我不希望活动播放器在我关闭通知时停止工作。发生这种情况是因为我正在服务中创建ExoPlayer实例,然后将实例传递给活动,因此当我关闭通知时,实例将丢失。是否有一种方法可以初始化活动中的播放器实例,然后将该实例传递给服务,这样我仍然可以从通知中控制视频,而不会在通知关闭后冒丢失实例的风险

我对安卓相当陌生,这是我第一次将服务绑定到活动,所以我真的不知道怎么做。我试着在谷歌上搜索,但也没用

这就是活动

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        playerView=findViewById(R.id.player_view);
        intent=new Intent(this,AudioPlayerService.class);

        //here i will add the url that needs to be loaded 
        //but at the moment this is just a draft

        Util.startForegroundService(this,intent);
        playerView.setUseController(true);
        //playerView.showController();
        playerView.setControllerAutoShow(true);
        playerView.setControllerHideOnTouch(true);
    }


    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            AudioPlayerService.LocalBinder binder = (AudioPlayerService.LocalBinder) iBinder;
            mService = binder.getService();
            mBound = true;
            initializePlayer();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mBound = false;
        }
    };

    @Override
    public void onStart() {
        super.onStart();
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        initializePlayer();
    }

    @Override
    protected void onStop() {
        unbindService(mConnection);
        mBound = false;
        super.onStop();
    }

    private void releasePlayer() {
        if (player != null) {
            player.release();
            player = null;
        }
    }


    private void initializePlayer() {
        if (mBound) {
            SimpleExoPlayer player = mService.getplayerInstance();
            playerView.setPlayer(player);
        }
    }
}
这就是服务

@Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public SimpleExoPlayer getplayerInstance() {
        if (player == null) {
            startPlayer();
        }
        return player;
    }

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

    @Override
    public void onCreate() {
        super.onCreate();
        final Context context=this;
    }

    private void startPlayer() {
        final Context context = this;
        player = ExoPlayerFactory.newSimpleInstance(context, new DefaultTrackSelector());
        ProgressiveMediaSource mediaSource = new ProgressiveMediaSource.Factory(new DefaultHttpDataSourceFactory("NotificationSync", 10000, 10000, true))
                .createMediaSource(Uri.parse("https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"));
        player.prepare(mediaSource);
        player.setPlayWhenReady(true);
        playerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(context, "1",
                R.string.app_name,
                2,
                new PlayerNotificationManager.MediaDescriptionAdapter() {
                    @Override
                    public String getCurrentContentTitle(Player player) {
                        return "title";
                    }

                    @Nullable
                    @Override
                    public PendingIntent createCurrentContentIntent(Player player) {
                        Intent intent = new Intent(context, MainActivity.class);
                        return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    }

                    @Nullable
                    @Override
                    public String getCurrentContentText(Player player) {
                        return "text";
                    }

                    @Nullable
                    @Override
                    public Bitmap getCurrentLargeIcon(Player player, PlayerNotificationManager.BitmapCallback callback) {
                        return null;
                    }

                }, new PlayerNotificationManager.NotificationListener() {
                    @Override
                    public void onNotificationCancelled(int notificationId, boolean dismissedByUser) {
                        stopSelf();

                    }

                    @Override
                    public void onNotificationPosted(int notificationId, Notification notification, boolean ongoing) {
                        mNotification = notification;
                        mNotificationId = notificationId;
                        if (ongoing) {
                            startForeground(notificationId, notification);
                        }
                    }

                }
        );
        playerNotificationManager.setPlayer(player);
        playerNotificationManager.setUseStopAction(true);
    }
    @Override
    public void onDestroy() {

        releasePlayer();
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (player == null) {

        //here i will get all the data from the intent that came from the 
        //activity (title,text,url...)

            startPlayer();
        }
        return START_STICKY;
    }

    private void releasePlayer() {
        if (player != null) {
            player.release();
            player = null;
        }
    }


}

最后,我想要实现的是一个视频播放器,您可以在活动中启动它,当我从“活动->背景”和“背景->活动”开始时,它也可以通过通知不间断地工作/控制。如果有其他方法可以实现这一点,我愿意尝试。

最后,我用另一种方法实现了这一点。最后,我用另一种方法实现了这一点