Esempio n. 1
0
        public static MediaPlaybackItem Create(object source, string title)
        {
            MediaPlaybackItem item = new MediaPlaybackItem();

            if (source != null && !string.IsNullOrEmpty(source.ToString()))
            {
                item.Source = new Uri(source.ToString());
                item.Title = title;
            }

            return item;
        }
Esempio n. 2
0
        public static MediaPlaybackItem Create(object source, object title, object position)
        {
            MediaPlaybackItem item = new MediaPlaybackItem();

            if(source != null && !string.IsNullOrEmpty(source.ToString()))
            {
                item.Source = new Uri(source.ToString());
                item.Title = title == null ? string.Empty : title.ToString();
                TimeSpan.TryParse(position.ToString(), out item.Position);
            }

            return item;
        }
Esempio n. 3
0
        /// <summary>
        /// Update Universal Volume Control (UVC) using SystemMediaTransPortControl APIs
        /// </summary>
        private void UpdateUVCOnNewTrack(MediaPlaybackItem item)
        {
            if (item == null)
            {
                smtc.PlaybackStatus = MediaPlaybackStatus.Stopped;
                smtc.DisplayUpdater.MusicProperties.Title = string.Empty;
                smtc.DisplayUpdater.Update();
                return;
            }

            smtc.PlaybackStatus = MediaPlaybackStatus.Playing;
            smtc.DisplayUpdater.Type = MediaPlaybackType.Music;
            smtc.DisplayUpdater.MusicProperties.Title = item.Title;

            var albumArtUri = item.Source;
            if (albumArtUri != null)
                smtc.DisplayUpdater.Thumbnail = RandomAccessStreamReference.CreateFromUri(albumArtUri);
            else
                smtc.DisplayUpdater.Thumbnail = null;

            smtc.DisplayUpdater.Update();
        }
Esempio n. 4
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 smtc_ButtonPressed(SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs args)
        {
            switch (args.Button)
            {
                case SystemMediaTransportControlsButton.Play:
                    Debug.WriteLine("UVC play button pressed");

                    // When the background task has been suspended and the SMTC
                    // starts it again asynchronously, some time is needed to let
                    // the task startup process in Run() complete.

                    // Wait for task to start.
                    // Once started, this stays signaled until shutdown so it won't wait
                    // again unless it needs to.
                    bool result = backgroundTaskStarted.WaitOne(5000);
                    if (!result)
                        throw new Exception("Background Task didnt initialize in time");

                    if (currentPlaybackItem == null)
                    {
                        var currentTrackId = ApplicationSettingsHelper.ReadResetSettingsValue(ApplicationSettingsConstants.TrackId);
                        var currentTitle = ApplicationSettingsHelper.ReadResetSettingsValue(ApplicationSettingsConstants.TrackTitle);
                        var currentTrackPosition = ApplicationSettingsHelper.ReadResetSettingsValue(ApplicationSettingsConstants.Position);

                        currentPlaybackItem = MediaPlaybackItem.Create(currentTrackId, currentTitle, currentTrackPosition);
                        playbackCurrentItemChanged(currentPlaybackItem);
                        currentPlaybackItem.Play();
                    } else
                    {
                        currentPlaybackItem.Resume();
                    }
                    break;
                case SystemMediaTransportControlsButton.Pause:
                    Debug.WriteLine("UVC pause button pressed");
                    try
                    {
                        currentPlaybackItem.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");
                    //SkipToPrevious();
                    break;
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Raised when playlist changes to a new track
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        void playbackCurrentItemChanged(MediaPlaybackItem item)
        {
            // Get the new item
            Debug.WriteLine("PlaybackList_CurrentItemChanged: " + (item == null ? "null" : GetTrackId(item).ToString()));

            // Update the system view
            UpdateUVCOnNewTrack(item);

            ApplicationSettingsHelper.SaveSettingsValue(
                ApplicationSettingsConstants.TrackId, item.Source.ToString()
            );

            ApplicationSettingsHelper.SaveSettingsValue(
                ApplicationSettingsConstants.Position, TimeSpan.Zero.ToString()
            );

            MessageService.SendMessageToForeground(new TrackChangedMessage(item.Source));
        }
Esempio n. 6
0
        Uri GetTrackId(MediaPlaybackItem item)
        {
            if (item == null)
                return null; // no track playing

            //return item.Source.CustomProperties[TrackIdKey] as Uri;
            return item.Source; ;
        }
Esempio n. 7
0
        public static bool TryCreate(object source, object title, object position, out MediaPlaybackItem item)
        {
            item = MediaPlaybackItem.Create(source, title, position);

            return item.Source != null;
        }