예제 #1
0
        //private void OnTimelinePropertiesChanged(MediaControlSession sender, TimelinePropertiesChangedEventArgs arg)
        //{
        //    TimelinePropertiesChanged?.Invoke(sender, arg);
        //}

        public TimeSpan CurrentPositon()
        {
            GlobalSystemMediaTransportControlsSessionTimelineProperties tp = CurrentSession()?.GetTimelineProperties();

            if (tp == null)
            {
                return(new TimeSpan());
            }

            return(tp.Position);
        }
예제 #2
0
        //Update the currently playing media
        async Task UpdateCurrentMediaInformation()
        {
            try
            {
                //Check if volume is currently muted
                bool currentOutputVolumeMuted = AudioMuteGetStatus(false);
                bool currentInputVolumeMuted  = AudioMuteGetStatus(true);
                AVActions.ActionDispatcherInvoke(delegate
                {
                    img_Main_VolumeMute.Visibility     = currentOutputVolumeMuted ? Visibility.Visible : Visibility.Collapsed;
                    img_Main_MicrophoneMute.Visibility = currentInputVolumeMuted ? Visibility.Visible : Visibility.Collapsed;
                });

                //Get the current audio volume and mute status
                string currentVolumeString = string.Empty;
                int    currentVolumeInt    = AudioVolumeGet(false);
                if (currentVolumeInt >= 0)
                {
                    currentVolumeString = "Volume " + currentVolumeInt + "%";
                    if (currentOutputVolumeMuted)
                    {
                        currentVolumeString += " (Muted)";
                    }
                }

                //Update the media and volume information
                AVActions.ActionDispatcherInvoke(delegate
                {
                    textblock_Volume_Level.Text = currentVolumeString;
                });

                //Get the media session manager
                GlobalSystemMediaTransportControlsSessionManager smtcSessionManager = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();

                if (smtcSessionManager == null)
                {
                    HideMediaInformation();
                    return;
                }

                //Get the current media session
                GlobalSystemMediaTransportControlsSession smtcSession = smtcSessionManager.GetCurrentSession();
                if (smtcSession == null)
                {
                    HideMediaInformation();
                    return;
                }

                GlobalSystemMediaTransportControlsSessionTimelineProperties mediaTimeline   = smtcSession.GetTimelineProperties();
                GlobalSystemMediaTransportControlsSessionMediaProperties    mediaProperties = await smtcSession.TryGetMediaPropertiesAsync();

                GlobalSystemMediaTransportControlsSessionPlaybackInfo mediaPlayInfo = smtcSession.GetPlaybackInfo();
                //Debug.WriteLine("Media: " + mediaProperties.Title + "/" + mediaProperties.Artist + "/" + mediaProperties.AlbumTitle + "/" + mediaProperties.Subtitle + "/" + mediaProperties.PlaybackType + "/" + mediaProperties.TrackNumber + "/" + mediaProperties.AlbumTrackCount);
                //Debug.WriteLine("Time: " + mediaTimeline.Position + "/" + mediaTimeline.StartTime + "/" + mediaTimeline.EndTime);
                //Debug.WriteLine("Play: " + mediaPlayInfo.PlaybackStatus + "/" + mediaPlayInfo.PlaybackType);

                //Load the media artist
                string mediaArtist = mediaProperties.Artist;
                if (string.IsNullOrWhiteSpace(mediaArtist))
                {
                    mediaArtist = mediaProperties.Subtitle;
                    if (string.IsNullOrWhiteSpace(mediaArtist))
                    {
                        mediaArtist = "Unknown artist";
                    }
                }

                //Load the track number
                string mediaTrackNumber   = string.Empty;
                int    currentTrackNumber = mediaProperties.TrackNumber;
                if (currentTrackNumber > 0)
                {
                    int totalTrackNumber = mediaProperties.AlbumTrackCount;
                    if (totalTrackNumber > 0)
                    {
                        mediaTrackNumber = "(" + currentTrackNumber + "/" + totalTrackNumber + ") ";
                    }
                    else
                    {
                        mediaTrackNumber = "(" + currentTrackNumber + ") ";
                    }
                }

                //Load the media title
                string mediaTitle = mediaProperties.Title;
                if (string.IsNullOrWhiteSpace(mediaTitle))
                {
                    mediaTitle = mediaTrackNumber + "Unknown title";
                }
                else
                {
                    mediaTitle = mediaTrackNumber + mediaTitle;
                }

                //Load the media album title
                string mediaAlbum = mediaProperties.AlbumTitle;
                if (string.IsNullOrWhiteSpace(mediaAlbum))
                {
                    AVActions.ActionDispatcherInvoke(delegate
                    {
                        button_Information_Album.Visibility = Visibility.Collapsed;
                    });
                }
                else
                {
                    AVActions.ActionDispatcherInvoke(delegate
                    {
                        button_Information_Album.Visibility = Visibility.Visible;
                    });
                }

                //Calculate the media progression
                double mediaProgress = 0;
                if (mediaTimeline.Position != new TimeSpan() && mediaTimeline.EndTime != new TimeSpan())
                {
                    mediaProgress = mediaTimeline.Position.TotalSeconds * 100 / mediaTimeline.EndTime.TotalSeconds;
                    AVActions.ActionDispatcherInvoke(delegate
                    {
                        button_Information_Progress.Visibility = Visibility.Visible;
                    });
                }
                else
                {
                    AVActions.ActionDispatcherInvoke(delegate
                    {
                        button_Information_Progress.Visibility = Visibility.Collapsed;
                    });
                }

                //Load the media thumbnail image
                BitmapFrame thumbnailBitmap = await GetMediaThumbnail(mediaProperties.Thumbnail);

                //Update the media and volume information
                AVActions.ActionDispatcherInvoke(delegate
                {
                    button_Information_Artist.Text    = mediaArtist;
                    button_Information_Title.Text     = mediaTitle;
                    button_Information_Album.Text     = mediaAlbum;
                    button_Information_Progress.Value = mediaProgress;
                    if (thumbnailBitmap != null)
                    {
                        button_Information_Thumbnail.Source = thumbnailBitmap;
                    }
                    else
                    {
                        button_Information_Thumbnail.Source = FileToBitmapImage(new string[] { "Assets/Default/Icons/Music.png" }, vImageSourceFolders, vImageBackupSource, IntPtr.Zero, -1, 0);
                    }
                    stackpanel_MediaPlaying.Visibility = Visibility.Visible;
                    stackpanel_MediaNone.Visibility    = Visibility.Collapsed;
                });
            }
            catch
            {
                //Debug.WriteLine("Failed updating playing media.");
                HideMediaInformation();
            }
        }