Exemplo n.º 1
0
        private void UpdateTimelineInfo(GlobalSystemMediaTransportControlsSession session)
        {
            try
            {
                var timeline = session.GetTimelineProperties();

                if (session.GetPlaybackInfo().Controls.IsPlaybackPositionEnabled&& timeline != null)
                {
                    TimelineStartTime = timeline.StartTime;
                    TimelineEndTime   = timeline.EndTime;
                    SetPlaybackPosition(timeline.Position);

                    IsTimelinePropertiesEnabled = true;
                }
                else
                {
                    TimelineStartTime = TimeSpan.Zero;
                    TimelineEndTime   = TimeSpan.Zero;
                    PlaybackPosition  = TimeSpan.Zero;

                    IsTimelinePropertiesEnabled = false;
                }
            }
            catch { }
        }
Exemplo n.º 2
0
        private async void Session_PlaybackInfoChanged(GlobalSystemMediaTransportControlsSession sender, PlaybackInfoChangedEventArgs args)
        {
            var    info   = session.GetPlaybackInfo();
            string status = "[PlaybackInfoChanged] PlaybackType: " + info.PlaybackType + ", Status: " + info.PlaybackStatus;

            Debug.WriteLine(status);
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { Status.Text = status; });
        }
Exemplo n.º 3
0
        private async void UpdateSessionInfo(GlobalSystemMediaTransportControlsSession session)
        {
            RaiseMediaPropertiesChanging();

            try
            {
                var mediaInfo = await session.TryGetMediaPropertiesAsync();

                if (mediaInfo != null)
                {
                    Title  = mediaInfo.Title;
                    Artist = mediaInfo.Artist;
                    TrackChangeDirection = (mediaInfo.TrackNumber - currentTrackNumber) switch
                    {
                        0 => MediaPlaybackTrackChangeDirection.Unknown,
                        > 0 => MediaPlaybackTrackChangeDirection.Forward,
                        < 0 => MediaPlaybackTrackChangeDirection.Backward
                    };

                    currentTrackNumber = mediaInfo.TrackNumber;
                }

                var playback = session.GetPlaybackInfo();

                if (playback != null)
                {
                    var playbackControls = playback.Controls;
                    IsPlayEnabled        = playbackControls.IsPlayEnabled;
                    IsPauseEnabled       = playbackControls.IsPauseEnabled;
                    IsPlayOrPauseEnabled = IsPlayEnabled || IsPauseEnabled;
                    IsPreviousEnabled    = playbackControls.IsPreviousEnabled;
                    IsNextEnabled        = playbackControls.IsNextEnabled;
                    IsShuffleEnabled     = playbackControls.IsShuffleEnabled;
                    IsRepeatEnabled      = playbackControls.IsRepeatEnabled;
                    IsStopEnabled        = playbackControls.IsStopEnabled;

                    PlaybackType = playback.PlaybackType switch
                    {
                        Windows.Media.MediaPlaybackType.Unknown => MediaPlaybackType.Unknown,
                        Windows.Media.MediaPlaybackType.Music => MediaPlaybackType.Music,
                        Windows.Media.MediaPlaybackType.Video => MediaPlaybackType.Video,
                        Windows.Media.MediaPlaybackType.Image => MediaPlaybackType.Image,
                        _ => throw new NotImplementedException()
                    };
                }

                UpdateTimelineInfo(session);

                UpdatePlaybackInfo(session);

                Thumbnail = await GetThumbnailImageSourceAsync(mediaInfo?.Thumbnail);
            }
            catch { }

            RaiseMediaPropertiesChanged();
        }
Exemplo n.º 4
0
 public async void UpdateMediaProperties(GlobalSystemMediaTransportControlsSession session, MediaPropertiesChangedEventArgs args)
 {
     await Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(() =>
     {
         if (session != null && session.GetPlaybackInfo() != null)
         {
             UpdateSessionInfo(session);
         }
     }));
 }
Exemplo n.º 5
0
            private void OnPlaybackInfoChanged(GlobalSystemMediaTransportControlsSession session, PlaybackInfoChangedEventArgs args = null)
            {
                var props = session.GetPlaybackInfo();

                if (props.PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Closed)
                {
                    session.PlaybackInfoChanged    -= OnPlaybackInfoChanged;
                    session.MediaPropertiesChanged -= OnSongChange;
                    RemoveSession(this);
                }
                else
                {
                    OnPlaybackStateChanged?.Invoke(this, props);
                }
            }
Exemplo n.º 6
0
        private void OnPlaybackInfoChanged(GlobalSystemMediaTransportControlsSession session, PlaybackInfoChangedEventArgs args = null)
        {
            if (CurrentSessions.ContainsKey(session))
            {
                var info      = session.GetPlaybackInfo();
                var isPlaying = info.PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing;

                var data = CurrentSessions[session];
                if (data.IsPlaying != isPlaying)
                {
                    data.IsPlaying           = isPlaying;
                    CurrentSessions[session] = data;
                    UpdateCurrentSong();
                }
            }
        }
 public async void updateTrackInfo(GlobalSystemMediaTransportControlsSession session = null, GlobalSystemMediaTransportControlsSessionMediaProperties currentTrack = null)
 {
     await Task.Run(async() =>
     {
         if (session == null)
         {
             session = await getSession();
         }
         GlobalSystemMediaTransportControlsSessionMediaProperties nextTrack;
         bool shouldContinue;
         do
         {
             Thread.Sleep(200);
             nextTrack      = await getMediaProperty(session);
             shouldContinue = nextTrack == null || isSameTrack(currentTrack, nextTrack);
         } while (shouldContinue);
         await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
         {
             var playStatus = session.GetPlaybackInfo().PlaybackStatus;
             bool isPlaying = playStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing;
             togglePlayButton(isPlaying);
             var mediaProp    = nextTrack;
             titleLabel.Text  = mediaProp.Title;
             artistLabel.Text = mediaProp.Artist;
             statusMutex.WaitOne();
             playbackStatus.title     = mediaProp.Title;
             playbackStatus.artist    = mediaProp.Artist;
             playbackStatus.album     = mediaProp.AlbumTitle;
             playbackStatus.playing   = isPlaying;
             playbackStatus.thumbnail = mediaProp.Thumbnail;
             statusMutex.ReleaseMutex();
             if (mediaProp.Thumbnail != null)
             {
                 try
                 {
                     var thumbnail = new BitmapImage();
                     await thumbnail.SetSourceAsync(await mediaProp.Thumbnail.OpenReadAsync());
                     albumArt.Source          = thumbnail;
                     var transform            = new ScaleTransform();
                     transform.CenterX        = -1;
                     albumArt.RenderTransform = transform;
                 }
                 catch { albumArt.Source = null; }
             }
         });
     });
 }
Exemplo n.º 8
0
 private void UpdatePlaybackInfo(GlobalSystemMediaTransportControlsSession session)
 {
     try
     {
         if (session != null)
         {
             var playback = session.GetPlaybackInfo();
             if (playback != null)
             {
                 IsPlaying       = playback.PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing;
                 IsShuffleActive = playback.IsShuffleActive;
                 AutoRepeatMode  = playback.AutoRepeatMode switch
                 {
                     Windows.Media.MediaPlaybackAutoRepeatMode.None => MediaPlaybackAutoRepeatMode.None,
                     Windows.Media.MediaPlaybackAutoRepeatMode.Track => MediaPlaybackAutoRepeatMode.Track,
                     Windows.Media.MediaPlaybackAutoRepeatMode.List => MediaPlaybackAutoRepeatMode.List,
                     _ => throw new NotImplementedException()
                 };
             }
         }
     }
     catch { }
 }
        public void UpdatePlaybackProperties()
        {
            try
            {
                var playbackInfo = Session.GetPlaybackInfo();

                if (playbackInfo != null)
                {
                    PreviousButton.IsEnabled = playbackInfo.Controls.IsPreviousEnabled;
                    NextButton.IsEnabled     = playbackInfo.Controls.IsNextEnabled;

                    if (playbackInfo.PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing)
                    {
                        PlayPauseButton.Content = "\uE769";
                    }
                    else if (playbackInfo.PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Paused)
                    {
                        PlayPauseButton.Content = "\uE768";
                    }
                }
            }
            catch (Exception) { }
        }
Exemplo n.º 10
0
        //Update the currently playing media
        async Task UpdateCurrentMediaInformation()
        {
            try
            {
                //Check if volume is currently muted
                bool currentOutputVolumeMuted = AudioMuteGetStatus(false);
                bool currentInputVolumeMuted  = AudioMuteGetStatus(true);
                await AVActions.ActionDispatcherInvokeAsync(delegate
                {
                    img_Main_VolumeMute.Visibility     = currentOutputVolumeMuted ? Visibility.Visible : Visibility.Collapsed;
                    img_Main_MicrophoneMute.Visibility = currentInputVolumeMuted ? Visibility.Visible : Visibility.Collapsed;
                });

                //Check if the application window is activated
                if (!vAppActivated)
                {
                    //Debug.WriteLine("Not updating media information, not activated.");
                    return;
                }

                //Check if the media popup is opened or setting is enabled
                bool mediaUpdateSetting = Convert.ToBoolean(Setting_Load(vConfigurationCtrlUI, "ShowMediaMain"));

                await AVActions.ActionDispatcherInvokeAsync(delegate
                {
                    if (!mediaUpdateSetting)
                    {
                        main_Media_Information.Visibility = Visibility.Collapsed;
                        //Debug.WriteLine("Not updating media information, disabled.");
                        return;
                    }
                    else
                    {
                        //Update the media information margin
                        double widthTopButtons        = stackpanel_TopButtons.ActualWidth + 10;
                        double widthClockBattery      = grid_ClockBattery.ActualWidth + grid_ClockBattery.Margin.Right + 10;
                        main_Media_Information.Margin = new Thickness(widthTopButtons, 10, widthClockBattery, 0);
                    }
                });

                //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;
                }

                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("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 media title
                string mediaTitle = mediaProperties.Title;
                if (string.IsNullOrWhiteSpace(mediaTitle))
                {
                    mediaTitle = "Unknown title";
                }

                //Update the media and volume information
                AVActions.ActionDispatcherInvoke(delegate
                {
                    main_Media_Information_Artist.Text = mediaArtist;
                    main_Media_Information_Title.Text  = " " + mediaTitle;
                    main_Media_Information.Visibility  = Visibility.Visible;

                    if (mediaPlayInfo.PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing)
                    {
                        main_Media_Information_Artist.Opacity = 1;
                        main_Media_Information_Title.Opacity  = 1;
                    }
                    else
                    {
                        main_Media_Information_Artist.Opacity = 0.40;
                        main_Media_Information_Title.Opacity  = 0.40;
                    }
                });
            }
            catch
            {
                //Debug.WriteLine("Failed updating playing media.");
                HideMediaInformation();
            }
        }
Exemplo n.º 11
0
        private async Task UpdateMediaInfo()
        {
            try
            {
                if (SpotifyMediaSession == null)
                {
                    throw new Exception();
                }
                GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties = await SpotifyMediaSession.TryGetMediaPropertiesAsync();

                GlobalSystemMediaTransportControlsSessionPlaybackControls mediaControls = SpotifyMediaSession.GetPlaybackInfo().Controls;

                CurrentArtist = mediaProperties.Artist.Length > 0 ? mediaProperties.Artist : mediaProperties.Title;
                IsAdPlaying   = !mediaControls.IsNextEnabled || mediaProperties.Title == "Advertisement";
                IsPlaying     = mediaControls.IsPauseEnabled;
            } catch (Exception e)
            {
                Debug.WriteLine("UpdateMediaInfo exception " + e.ToString());
                CurrentArtist = "N/A";
                IsAdPlaying   = false;
                IsPlaying     = false;
                await RegisterSpotifyMediaSession(true);
            }
        }
Exemplo n.º 12
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();
            }
        }