private void GSMTCSession_TimelinePropertiesChanged(GlobalSystemMediaTransportControlsSession session, TimelinePropertiesChangedEventArgs args)
 {
     Application.Current.Dispatcher.Invoke(() =>
     {
         UpdateTimelineInfo(session);
     });
 }
 private async void MediaPropertiesChanged(GlobalSystemMediaTransportControlsSession sender, MediaPropertiesChangedEventArgs args)
 {
     await Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(() =>
     {
         UpdateMediaProperties();
     }));
 }
示例#3
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 { }
        }
 private void GSMTCSession_PlaybackInfoChanged(GlobalSystemMediaTransportControlsSession session, PlaybackInfoChangedEventArgs args)
 {
     Application.Current.Dispatcher.Invoke(() =>
     {
         UpdatePlaybackInfo(session);
     });
 }
示例#5
0
 private async void GSMTCSession_TimelinePropertiesChanged(GlobalSystemMediaTransportControlsSession session, TimelinePropertiesChangedEventArgs args)
 {
     await Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() =>
     {
         UpdateTimelineInfo(session);
     }));
 }
示例#6
0
        public static void Reload()
        {
            if (_smtcManager != null)
            {
                _smtcManager.CurrentSessionChanged -= SmtcManagerOnCurrentSessionChanged;
                _smtcManager.SessionsChanged       -= SmtcManagerOnSessionsChanged;
            }

            if (_smtc != null)
            {
                _smtc.MediaPropertiesChanged    -= SmtcOnMediaPropertiesChanged;
                _smtc.TimelinePropertiesChanged -= SmtcOnTimelinePropertiesChanged;
            }

            //TODO: Implement automatic switching to new playing smtc

            _smtcManager = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetAwaiter().GetResult();
            _smtcManager.SessionsChanged       += SmtcManagerOnSessionsChanged;
            _smtcManager.CurrentSessionChanged += SmtcManagerOnCurrentSessionChanged;
            _smtc = _smtcManager.GetCurrentSession();
            if (_smtc != null)
            {
                _smtc.MediaPropertiesChanged    += SmtcOnMediaPropertiesChanged;
                _smtc.TimelinePropertiesChanged += SmtcOnTimelinePropertiesChanged;
                _mediaProperties = _smtc.TryGetMediaPropertiesAsync().GetAwaiter().GetResult();
                UpdateMediaProperties();
            }
        }
        private async Task RegisterSpotifyMediaSession(bool unmute)
        {
            if (SessionManager == null)
            {
                SessionManager = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();
            }
            List <GlobalSystemMediaTransportControlsSession> sessions = new List <GlobalSystemMediaTransportControlsSession>();

            sessions.Add(SessionManager.GetCurrentSession());
            sessions.AddRange(SessionManager.GetSessions());
            foreach (GlobalSystemMediaTransportControlsSession session in sessions)
            {
                if (session != null && session.SourceAppUserModelId == "Spotify.exe")
                {
                    Debug.WriteLine("Registering " + session.GetHashCode());
                    if (unmute)
                    {
                        AudioUtils.SetSpotifyMute(false);
                    }
                    SpotifyMediaSession = session;
                    SpotifyMediaSession.MediaPropertiesChanged += async(s, args) =>
                    {
                        await UpdateMediaInfo();
                    };
                    return;
                }
            }
            SpotifyMediaSession = null;
        }
示例#8
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; });
        }
示例#9
0
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            manager = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();

            session = manager.GetCurrentSession();
            session.MediaPropertiesChanged += Session_MediaPropertiesChanged;
            session.PlaybackInfoChanged    += Session_PlaybackInfoChanged;
        }
示例#10
0
        //TODO: This does not trigger
        private static void SmtcOnTimelinePropertiesChanged(GlobalSystemMediaTransportControlsSession sender, TimelinePropertiesChangedEventArgs args)
        {
            var pos = sender.GetTimelineProperties().Position.TotalSeconds;
            var max = sender.GetTimelineProperties().EndTime.TotalSeconds;

            var frac = pos / max * 100;

            MainWindow.Window.Dispatcher?.InvokeAsync(new Action(() => MainWindow.Window.Media_ProgressBar.Value = frac));
        }
示例#11
0
        private async void Session_MediaPropertiesChanged(GlobalSystemMediaTransportControlsSession sender, MediaPropertiesChangedEventArgs args)
        {
            properties = await session.TryGetMediaPropertiesAsync();

            string status = "[MediaPropertiesChanged] Artist: " + properties.Artist + ", Title: " + properties.Title;

            Debug.WriteLine(status);
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { Status.Text = status; });
        }
        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();
        }
示例#13
0
 public async void UpdateMediaProperties(GlobalSystemMediaTransportControlsSession session, MediaPropertiesChangedEventArgs args)
 {
     await Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(() =>
     {
         if (session != null && session.GetPlaybackInfo() != null)
         {
             UpdateSessionInfo(session);
         }
     }));
 }
示例#14
0
 public void Dispose()
 {
     if (GSMTCSession != null)
     {
         GSMTCSession.MediaPropertiesChanged    -= GSMTCSession_MediaPropertiesChanged;
         GSMTCSession.PlaybackInfoChanged       -= GSMTCSession_PlaybackInfoChanged;
         GSMTCSession.TimelinePropertiesChanged -= GSMTCSession_TimelinePropertiesChanged;
     }
     GSMTCSession = null;
 }
        public MusicSessionControl(GlobalSystemMediaTransportControlsSession session)
        {
            Session = session;
            InitializeComponent();

            Session.MediaPropertiesChanged += MediaPropertiesChanged;
            Session.PlaybackInfoChanged    += PlaybackInfoChanged;
            UpdateMediaProperties();
            UpdatePlaybackProperties();
        }
        private void CurrentSession_PlaybackInfoChanged(GlobalSystemMediaTransportControlsSession sender, Windows.Media.Control.PlaybackInfoChangedEventArgs args)
        {
            Console.WriteLine("Playback status changed.");

            var newPlaybackStatus = GetPlaybackStatus();

            OnPlaybackStatusChanged(new PlaybackStatusEventArgs()
            {
                PlaybackStatus = newPlaybackStatus
            });
        }
        private async void CurrentSession_MediaPropertiesChanged(GlobalSystemMediaTransportControlsSession sender, MediaPropertiesChangedEventArgs args)
        {
            Console.WriteLine("Media properties changed.");

            var newMediaInfo = await GetMediaInfo();

            OnMediaInfoChanged(new MediaInfoChangedEventArgs()
            {
                MediaInfo = newMediaInfo
            });
        }
示例#18
0
        /// <inheritdoc/>
        public bool IsMatch(GlobalSystemMediaTransportControlsSession session)
        {
            var process = Process.GetProcessById((int)this.SourceProciessId);

            if (process == null)
            {
                return(false);
            }

            return(new ProcessNamePredicate(process.ProcessName)
                   .IsMatch(session));
        }
        public override void Disconnect()
        {
            if (GSMTCSession != null)
            {
                GSMTCSession.MediaPropertiesChanged    -= GSMTCSession_MediaPropertiesChanged;
                GSMTCSession.PlaybackInfoChanged       -= GSMTCSession_PlaybackInfoChanged;
                GSMTCSession.TimelinePropertiesChanged -= GSMTCSession_TimelinePropertiesChanged;
            }
            GSMTCSession = null;

            sourceAppInfo.Dispose();
            sourceAppInfo = null;
        }
示例#20
0
            internal async void OnSongChange(GlobalSystemMediaTransportControlsSession session, MediaPropertiesChangedEventArgs args = null)
            {
                var props = await session.TryGetMediaPropertiesAsync();

                string song = $"{props.Title} | {props.Artist}";

                //This is needed because for some reason this method is invoked twice every song change
                if (LastSong != song && !(String.IsNullOrWhiteSpace(props.Title) && String.IsNullOrWhiteSpace(props.Artist)))
                {
                    LastSong = song;
                    OnSongChanged?.Invoke(this, props);
                }
            }
示例#21
0
        private static void GlobalSystemMediaTransportControlsSessionManager_CurrentSessionChanged(GlobalSystemMediaTransportControlsSessionManager sender, CurrentSessionChangedEventArgs args)
        {
            if (globalSystemMediaTransportControlsSession != null)
            {
                globalSystemMediaTransportControlsSession.MediaPropertiesChanged -= GlobalSystemMediaTransportControlsSession_MediaPropertiesChanged;
            }

            globalSystemMediaTransportControlsSession = globalSystemMediaTransportControlsSessionManager.GetCurrentSession();

            if (globalSystemMediaTransportControlsSession != null)
            {
                globalSystemMediaTransportControlsSession.MediaPropertiesChanged += GlobalSystemMediaTransportControlsSession_MediaPropertiesChanged;
            }
        }
示例#22
0
 //TODO: this does not trigger
 private static void SmtcManagerOnCurrentSessionChanged(GlobalSystemMediaTransportControlsSessionManager sender, CurrentSessionChangedEventArgs args)
 {
     if (_smtc != null)
     {
         _smtc.MediaPropertiesChanged    -= SmtcOnMediaPropertiesChanged;
         _smtc.TimelinePropertiesChanged -= SmtcOnTimelinePropertiesChanged;
     }
     _smtc = sender.GetCurrentSession();
     if (_smtc != null)
     {
         _smtc.MediaPropertiesChanged += SmtcOnMediaPropertiesChanged;
         _mediaProperties              = _smtc.TryGetMediaPropertiesAsync().GetAwaiter().GetResult();
     }
 }
示例#23
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);
                }
            }
示例#24
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; }
             }
         });
     });
 }
        public async Task Initialize()
        {
            // API courtesy of https://stackoverflow.com/a/63099881.
            var sessionManager = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();

            var currentSession = sessionManager.GetCurrentSession();

            if (currentSession == null)
            {
                throw new Exception("Unable to obtain current session.");
            }

            this.currentSession = currentSession;
            currentSession.MediaPropertiesChanged += CurrentSession_MediaPropertiesChanged;
            currentSession.PlaybackInfoChanged    += CurrentSession_PlaybackInfoChanged;
            isInitialized = true;
        }
示例#27
0
        private void OnMediaPropertiesChanged(GlobalSystemMediaTransportControlsSession session, MediaPropertiesChangedEventArgs args = null)
        {
            SimpleLogger.DefaultLog("MediaSessionManager::OnMediaPropertiesChanged called...");
            try {
                if (CurrentSessions.ContainsKey(session))
                {
                    var   props    = session.TryGetMediaPropertiesAsync().GetAwaiter().GetResult();
                    var   title    = props.Title;
                    var   artist   = props.Artist;
                    Image albumArt = null;
                    if (props.Thumbnail != null)
                    {
                        using (var winStream = props.Thumbnail.OpenReadAsync().GetAwaiter().GetResult()) {
                            using (var stream = winStream.AsStream()) {
                                if (session.SourceAppUserModelId.ToLower().Contains("spotify"))
                                {
                                    using (var image = Image.FromStream(stream)) {
                                        using (var bitmap = new Bitmap(image)) {
                                            albumArt = bitmap.Clone(new Rectangle(33, 0, 234, 234), bitmap.PixelFormat);
                                        }
                                    }
                                }
                                else
                                {
                                    albumArt = Image.FromStream(stream);
                                }
                            }
                        }
                    }

                    var data = CurrentSessions[session];
                    if (data.Title != title || data.Artist != artist || data.AlbumArt != albumArt)
                    {
                        data.Title               = title;
                        data.Artist              = artist;
                        data.AlbumArt            = albumArt;
                        CurrentSessions[session] = data;
                        UpdateCurrentSong();
                    }
                }
            } catch (Exception e) {
                SimpleLogger.DefaultLog($"MediaSessionManager::OnMediaPropertiesChanged - Exception - {e.Message}\n{e.StackTrace}");
            }
            SimpleLogger.DefaultLog("MediaSessionManager::OnMediaPropertiesChanged DONE");
        }
示例#28
0
        private async void UpdateSessionInfo(GlobalSystemMediaTransportControlsSession session)
        {
            try
            {
                var mediaInfo = await session.TryGetMediaPropertiesAsync();

                if (mediaInfo.Artist != "")
                {
                    MusicPlaying.Text = mediaInfo.Artist + " - " + mediaInfo.Title;
                }
                else
                {
                    MusicPlaying.Text = mediaInfo.Title;
                }
            }
            catch (Exception)
            {
            }
        }
示例#29
0
        public static async void Start()
        {
            globalSystemMediaTransportControlsSessionManager = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();

            globalSystemMediaTransportControlsSession = globalSystemMediaTransportControlsSessionManager.GetCurrentSession();

            SetPlaybackInfoMediaProperties();

            globalSystemMediaTransportControlsSessionManager.CurrentSessionChanged += GlobalSystemMediaTransportControlsSessionManager_CurrentSessionChanged;

            if (globalSystemMediaTransportControlsSession != null)
            {
                GlobalSystemMediaTransportControlsSession_MediaPropertiesChanged(null, null);

                globalSystemMediaTransportControlsSession.MediaPropertiesChanged += GlobalSystemMediaTransportControlsSession_MediaPropertiesChanged;
            }

            started = true;
        }
示例#30
0
        public async void SetupNowPlaying()
        {
            SMTC = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();

            CurrentSession = SMTC.GetCurrentSession();

            if (CurrentSession != null)
            {
                UpdateSessionInfo(CurrentSession);
                CurrentSession.MediaPropertiesChanged += UpdateMediaProperties;
                MainButton.Visibility = Visibility.Visible;
            }
            else
            {
                MainButton.Visibility = Visibility.Collapsed;
            }

            SMTC.CurrentSessionChanged += SMTC_CurrentSessionChanged;
            Flyout = new MusicFlyout(SMTC);
        }