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

时间:2019-07-16 11:09:21

标签: service notifications instance bind exoplayer

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

我对android很陌生,这是我第一次将服务绑定到活动,所以我真的不知道该怎么做。我尝试在Google上进行搜索,但这也没有帮助。

这是活动

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


}

最后,我要实现的是一个视频播放器,您可以在活动中启动它,并且当我从活动->背景和背景->活动中去时,也可以通过通知进行工作/不受干扰地进行控制。如果还有其他方法可以实现,我可以尝试。

0 个答案:

没有答案