Esempio n. 1
0
        public PlaybackManager(IPlaybackServiceCallback serviceCallback, Resources resources,
                               MusicProvider musicProvider, QueueManager queueManager,
                               IPlayback playback)
        {
            this.musicProvider   = musicProvider;
            this.serviceCallback = serviceCallback;
            this.resources       = resources;
            this.queueManager    = queueManager;

            mediaSessionCallback.OnPlayImpl = () =>
            {
                LogHelper.Debug(Tag, "play");
                if (queueManager.CurrentMusic == null)
                {
                    queueManager.SetRandomQueue();
                }
                HandlePlayRequest();
            };

            mediaSessionCallback.OnSkipToQueueItemImpl = (long queueId) =>
            {
                LogHelper.Debug(Tag, "OnSkipToQueueItem:" + queueId);
                queueManager.SetCurrentQueueItem(queueId);
                HandlePlayRequest();
                queueManager.UpdateMetadata();
            };

            mediaSessionCallback.OnSeekToImpl = (long position) =>
            {
                LogHelper.Debug(Tag, "onSeekTo:", position);
                playback.SeekTo((int)position);
            };

            mediaSessionCallback.OnPlayFromMediaIdImpl = (string mediaId, Bundle extras) =>
            {
                LogHelper.Debug(Tag, "playFromMediaId mediaId:", mediaId, "  extras=", extras);
                queueManager.SetQueueFromMusic(mediaId);
                HandlePlayRequest();
            };

            mediaSessionCallback.OnPauseImpl = () =>
            {
                LogHelper.Debug(Tag, "pause. current state=" + Playback.State);
                HandlePauseRequest();
            };

            mediaSessionCallback.OnStopImpl = () =>
            {
                LogHelper.Debug(Tag, "stop. current state=" + Playback.State);
                HandleStopRequest(null);
            };

            mediaSessionCallback.OnSkipToNextImpl = () =>
            {
                LogHelper.Debug(Tag, "skipToNext");
                if (queueManager.SkipQueuePosition(1))
                {
                    HandlePlayRequest();
                }
                else
                {
                    HandleStopRequest("Cannot skip");
                }
                queueManager.UpdateMetadata();
            };

            mediaSessionCallback.OnSkipToPreviousImpl = () =>
            {
                if (queueManager.SkipQueuePosition(-1))
                {
                    HandlePlayRequest();
                }
                else
                {
                    HandleStopRequest("Cannot skip");
                }
                queueManager.UpdateMetadata();
            };

            mediaSessionCallback.OnPlayFromSearchImpl = (string query, Bundle extras) =>
            {
                LogHelper.Debug(Tag, "playFromSearch  query=", query, " extras=", extras);

                Playback.State = PlaybackStateCompat.StateConnecting;
                queueManager.SetQueueFromSearch(query, extras);
                HandlePlayRequest();
                queueManager.UpdateMetadata();
            };

            Playback          = playback;
            Playback.Callback = this;
        }