예제 #1
0
        public override void OnCreate()
        {
            base.OnCreate();
            LogHelper.Debug(Tag, "onCreate");

            musicProvider = new MusicProvider();

            var context = ApplicationContext;

            var hasReadExternalStoragePermission = ActivityCompat.CheckSelfPermission(context, Manifest.Permission.ReadExternalStorage);

            if (hasReadExternalStoragePermission == Permission.Granted)
            {
                musicProvider.RetrieveMediaAsync(context, null);
            }
            else
            {
                var msg = Resources.GetString(Resource.String.permission_denied_msg, "READ_EXTERNAL_STORAGE");
                Toast.MakeText(context, msg, ToastLength.Short).Show();
            }

            packageValidator = new PackageValidator(this);

            lightOrganProcessor = new LightOrganProcessor();
            lightOrganProcessor.LightOrganDataUpdated += (s, e) =>
            {
                OnLightOrganDataUpdated(e.Data);
            };

            var queueManager = new QueueManager(musicProvider, Resources,
                                                new QueueManager.MetadataUpdateListener
            {
                OnMetadataChanged = (metadata) =>
                {
                    session.SetMetadata(metadata);
                },

                OnMetadataRetrieveError = () =>
                {
                    playbackManager.UpdatePlaybackState(Resources.GetString(Resource.String.no_metadata_msg));
                },

                OnCurrentQueueIndexUpdated = (queueIndex) =>
                {
                    playbackManager.HandlePlayRequest();
                },

                OnQueueUpdated = (title, newQueue) =>
                {
                    session.SetQueue(newQueue);
                    session.SetQueueTitle(title);
                }
            });

            var hasRecordAudioPermission = ActivityCompat.CheckSelfPermission(context, Manifest.Permission.RecordAudio);

            if (hasRecordAudioPermission != Permission.Granted)
            {
                var msg = Resources.GetString(Resource.String.permission_denied_msg, "RECORD_AUDIO");
                Toast.MakeText(context, msg, ToastLength.Short).Show();
            }

            var playback = new LocalPlayback(this, musicProvider, hasRecordAudioPermission == Permission.Granted);

            playbackManager = new PlaybackManager(this, Resources, musicProvider, queueManager, playback);

            // Start a new MediaSession
            session      = new MediaSessionCompat(this, "MusicService");
            SessionToken = session.SessionToken;
            session.SetCallback(playbackManager.GetMediaSessionCallback());
            session.SetFlags(MediaSessionCompat.FlagHandlesMediaButtons |
                             MediaSessionCompat.FlagHandlesTransportControls);

            var intent = new Intent(context, typeof(MainActivity));
            var pi     = PendingIntent.GetActivity(context, 99 /*request code*/,
                                                   intent, PendingIntentFlags.UpdateCurrent);

            session.SetSessionActivity(pi);

            playbackManager.UpdatePlaybackState(null);

            try
            {
                mediaNotificationManager = new MediaNotificationManager(this);
            }
            catch (Exception e)
            {
                throw new Exception("Could not create a MediaNotificationManager", e);
            }
        }