Ejemplo n.º 1
0
        /// <summary>
        ///     This function controls the button events from UVC.
        ///     This code if not run in background process, will not be able to handle button pressed events when app is suspended.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void systemmediatransportcontrol_ButtonPressed(SystemMediaTransportControls sender,
                                                               SystemMediaTransportControlsButtonPressedEventArgs args)
        {
            switch (args.Button)
            {
            case SystemMediaTransportControlsButton.Play:
                Debug.WriteLine("UVC play button pressed");
                // If music is in paused state, for a period of more than 5 minutes,
                //app will get task cancellation and it cannot run code.
                //However, user can still play music by pressing play via UVC unless a new app comes in clears UVC.
                //When this happens, the task gets re-initialized and that is asynchronous and hence the wait
                if (!_backgroundtaskrunning)
                {
                    var result = TaskStarted.WaitOne(5000);
                    if (!result)
                    {
                        throw new Exception("Background Task didn't initialize in time");
                    }
                    StartPlayback();
                }
                else
                {
                    BackgroundMediaPlayer.Current.Play();
                }
                break;

            case SystemMediaTransportControlsButton.Pause:
                Debug.WriteLine("UVC pause button pressed");
                try
                {
                    BackgroundMediaPlayer.Current.Pause();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                }
                break;

            case SystemMediaTransportControlsButton.Next:
                Debug.WriteLine("UVC next button pressed");
                SkipToNext();
                break;

            case SystemMediaTransportControlsButton.Previous:
                Debug.WriteLine("UVC previous button pressed");
                if (BackgroundMediaPlayer.Current.Position.TotalSeconds > 20)
                {
                    //StartPlayback();
                    BackgroundMediaPlayer.Current.Position = TimeSpan.Zero;
                    BackgroundMediaPlayer.Current.Play();
                }
                else
                {
                    SkipToPrevious();
                }

                break;
            }
        }