Exemplo n.º 1
0
        /// <summary>
        /// Sends message to background informing app has resumed
        /// Subscribe to MediaPlayer events
        /// </summary>
        void ForegroundApp_Resuming(object sender, object e)
        {
            ApplicationSettingsHelper.SaveSettingsValue(ApplicationSettingsConstants.AppState, AppState.Active.ToString());

            // Verify the task is running
            if (IsMyBackgroundTaskRunning)
            {
                // If yes, it's safe to reconnect to media play handlers
                AddMediaPlayerEventHandlers();

                // Send message to background task that app is resumed so it can start sending notifications again
                MessageService.SendMessageToBackground(new AppResumedMessage());

                UpdateTransportControls(CurrentPlayer.CurrentState);

                int trackId = GetCurrentTrackIdAfterAppResume();

                CurrentTrack = trackId == 0 ? null : TrackQueue.Single(s => s.InternalID == trackId);
            }
            else
            {
                StopButtonVisibility = Visibility.Collapsed;
                PlayButtonVisibility = Visibility.Visible;
                PlayButtonIsEnabled  = true;
            }

            RefreshBindings();
        }
Exemplo n.º 2
0
        /// <summary>
        /// This event is raised when a message is recieved from BackgroundAudioTask
        /// </summary>
        async void BackgroundMediaPlayer_MessageReceivedFromBackground(object sender, MediaPlayerDataReceivedEventArgs e)
        {
            TrackChangedMessage trackChangedMessage;

            if (MessageService.TryParseMessage(e.Data, out trackChangedMessage))
            {
                // When foreground app is active change track based on background message
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    // If playback stopped then clear the UI
                    if (trackChangedMessage.InternalMixID == 0)
                    {
                        Stop();
                        RefreshBindings();

                        return;
                    }

                    CurrentTrack = TrackQueue.Single(s => s.InternalID == trackChangedMessage.InternalMixID);

                    // Ensure track buttons are re-enabled since they are disabled when pressed
                    foreach (Mix m in TrackQueue.Where(m => m.Playing))
                    {
                        m.Playing = false;
                    }

                    CurrentTrack.Playing = true;

                    NextButtonIsEnabled     = true;
                    PreviousButtonIsEnabled = true;
                    RefreshBindings();
                });

                return;
            }

            BackgroundAudioTaskStartedMessage backgroundAudioTaskStartedMessage;

            if (MessageService.TryParseMessage(e.Data, out backgroundAudioTaskStartedMessage))
            {
                // StartBackgroundAudioTask is waiting for this signal to know when the task is up and running
                // and ready to receive messages
                Debug.WriteLine("BackgroundAudioTask started");
                return;
            }

            UpdateMediaPlayerInfoMessage updateMediaPlayerInfoMessage;

            if (MessageService.TryParseMessage(e.Data, out updateMediaPlayerInfoMessage))
            {
                // StartBackgroundAudioTask is waiting for this signal to know when the task is up and running
                // and ready to receive messages
                // When foreground app is active change track based on background message
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                {
                    if (MainpageViewModel.instance.GetMixesTask != null && MainpageViewModel.instance.GetMixesTask.IsCompleted == false)
                    {
                        await MainpageViewModel.instance.GetMixesTask;
                    }

                    if (updateMediaPlayerInfoMessage.InternalMixID != 0)
                    {
                        if (TrackQueue == null)
                        {
                            TrackQueue = MainpageViewModel.instance.Mixes;
                        }

                        Mix PlayingTrack = CurrentTrack;
                        CurrentTrack     = TrackQueue.Where(s => s.InternalID == updateMediaPlayerInfoMessage.InternalMixID).FirstOrDefault();

                        if (CurrentTrack == null)
                        {
                            CurrentTrack = PlayingTrack;
                        }

                        // Ensure track buttons are re-enabled since they are disabled when pressed
                        foreach (Mix m in TrackQueue.Where(m => m.Playing))
                        {
                            if (m != CurrentTrack)
                            {
                                m.Playing = false;
                            }
                        }

                        if (CurrentTrack != null)
                        {
                            CurrentTrack.Playing = true;

                            NextButtonIsEnabled     = true;
                            PreviousButtonIsEnabled = true;
                            RefreshBindings();
                        }
                    }
                });

                Debug.WriteLine("Updating MediaInfo");
                return;
            }
        }