public async Task UpdateTrackQueue()
        {
            TrackQueue = await MixDataHandler.instance.GetMixes(true);

            if (CurrentTrack != null && CurrentTrack.Playing)
            {
                CurrentTrack = TrackQueue.Where(x => x.InternalID == CurrentTrack.InternalID).FirstOrDefault();

                if (CurrentTrack != null)
                {
                    CurrentTrack.Playing = true;
                }
            }
        }
Ejemplo 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;
            }
        }