Esempio n. 1
0
        public MainPage()
        {
            this.InitializeComponent();
            prevButton.Click      += PrevButton_Click;
            nextButton.Click      += NextButton_Click;
            playPauseButton.Click += PlayPauseButton_Click;
            WriteCurrentlyPlayingAsync();
            makeSmall();
            // using Windows.UI.ViewManagement;

            var titleBar = ApplicationView.GetForCurrentView().TitleBar;

            // Set colors
            titleBar.ForegroundColor               = Windows.UI.Colors.Black;
            titleBar.BackgroundColor               = Windows.UI.Colors.Black;
            titleBar.ButtonForegroundColor         = Windows.UI.Colors.White;
            titleBar.ButtonBackgroundColor         = Windows.UI.Colors.Black;
            titleBar.InactiveForegroundColor       = Windows.UI.Colors.Black;
            titleBar.InactiveBackgroundColor       = Windows.UI.Colors.Black;
            titleBar.ButtonInactiveForegroundColor = Windows.UI.Colors.White;
            titleBar.ButtonInactiveBackgroundColor = Windows.UI.Colors.Black;

            var sessionManager = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetAwaiter().GetResult();

            sessionManager.CurrentSessionChanged += SessionManager_CurrentSessionChanged;
            sessionManager.SessionsChanged       += SessionManager_SessionsChanged;
        }
Esempio n. 2
0
        public static GlobalSystemMediaTransportControlsSession GetSession()
        {
            var sessions = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetAwaiter().GetResult();

            return(sessions.GetSessions()
                   .FirstOrDefault(x => x.SourceAppUserModelId == "Spotify.exe"));
        }
Esempio n. 3
0
        //Get palying details
        private static GlobalSystemMediaTransportControlsSessionMediaProperties GetStuff()
        {
            var gsmtcsm = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetAwaiter().GetResult()
                          .GetCurrentSession();

            return(gsmtcsm.TryGetMediaPropertiesAsync().GetAwaiter().GetResult());
        }
Esempio n. 4
0
        private void OnSessionsChanged(GlobalSystemMediaTransportControlsSessionManager sender, SessionsChangedEventArgs args = null)
        {
            DisableUpdates = true;

            SimpleLogger.DefaultLog("MediaSessionManager::OnSessionsChanged - Clearing old sessions...");
            foreach (var entry in CurrentSessions)
            {
                entry.Key.MediaPropertiesChanged -= OnMediaPropertiesChanged;
                entry.Key.PlaybackInfoChanged    -= OnPlaybackInfoChanged;
            }
            CurrentSessions.Clear();
            SimpleLogger.DefaultLog("MediaSessionManager::OnSessionsChanged - Clearing old sessions DONE");

            SimpleLogger.DefaultLog("MediaSessionManager::OnSessionsChanged - Adding new sessions...");
            var sessions = sender.GetSessions();

            foreach (var session in sessions)
            {
                CurrentSessions[session] = new PlaybackData {
                    IsPlaying = false,
                    Artist    = "",
                    Title     = "",
                };
                OnMediaPropertiesChanged(session);
                OnPlaybackInfoChanged(session);
                session.MediaPropertiesChanged += OnMediaPropertiesChanged;
                session.PlaybackInfoChanged    += OnPlaybackInfoChanged;
                SimpleLogger.DefaultLog($"    {session.SourceAppUserModelId}");
            }
            SimpleLogger.DefaultLog("MediaSessionManager::OnSessionsChanged - Adding new sessions DONE");

            DisableUpdates = false;
            UpdateCurrentSong();
        }
Esempio n. 5
0
        public MusicFlyout(GlobalSystemMediaTransportControlsSessionManager smtc)
        {
            InitializeComponent();

            SMTC = smtc;
            Setup();
        }
Esempio n. 6
0
        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;
        }
Esempio n. 7
0
        private async void SetupSMTCAsync()
        {
            //GSMTC

            SMTC = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();

            var sessions = SMTC.GetSessions();

            foreach (var session in sessions)
            {
                var covfefe = new SessionControl
                {
                    SMTCSession = session,
                    Margin      = new Thickness(0, 2, 0, 0)
                };

                SessionsStackPanel.Children.Add(covfefe);
            }

            if (SessionsStackPanel.Children.Count > 0)
            {
                SessionsStackPanel.Margin = new Thickness(0, 2, 0, 0);
                (SessionsStackPanel.Children[0] as SessionControl).Margin = new Thickness(0);
            }
            else
            {
                SessionsStackPanel.Margin = new Thickness(0);
            }

            SMTC.SessionsChanged += SMTC_SessionsChanged;
        }
Esempio n. 8
0
        private void SMTC_SessionsChanged(GlobalSystemMediaTransportControlsSessionManager sender, SessionsChangedEventArgs args)
        {
            Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(() =>
            {
                var lol = args.GetType();

                var sessions = SMTC.GetSessions();

                var CLR = sessions.ToList();

                // DELETE IF IT DOESN'T EXIST ANYMORE.

                var toDelete = new List <SessionControl>();

                //Find if we have the mythical beast already in list.
                foreach (var sessControl in SessionsStackPanel.Children)
                {
                    var tmp = (SessionControl)sessControl;
                    //if (!sessions.Any(s => s.SourceAppUserModelId == tmp.SMTCSession.SourceAppUserModelId)) //Sad this doesn't work if there are two SourceAppUserModelId with the same name :(
                    toDelete.Add(tmp);
                }

                toDelete.ForEach(ses =>
                {
                    ses.SMTCSession = null;
                    SessionsStackPanel.Children.Remove(ses);
                });


                //ADD IF IT'S NEW

                var toAdd = new List <SessionControl>();

                foreach (var session in sessions)
                {
                    if (!SessionsStackPanel.Children.Cast <SessionControl>().Any(s => s.SMTCSession.SourceAppUserModelId == session.SourceAppUserModelId))
                    {
                        toAdd.Add(new SessionControl
                        {
                            SMTCSession = session,
                            Margin      = new Thickness(0, 2, 0, 0)
                        });
                    }
                }

                toAdd.ForEach(ses => SessionsStackPanel.Children.Add(ses));


                if (SessionsStackPanel.Children.Count > 0)
                {
                    SessionsStackPanel.Margin = new Thickness(0, 2, 0, 0);
                    (SessionsStackPanel.Children[0] as SessionControl).Margin = new Thickness(0);
                }
                else
                {
                    SessionsStackPanel.Margin = new Thickness(0);
                }
            }));
        }
Esempio n. 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;
        }
 public async Task <GlobalSystemMediaTransportControlsSession> getSession()
 {
     try
     {
         return((await GlobalSystemMediaTransportControlsSessionManager.RequestAsync())
                .GetCurrentSession());
     }
     catch { return(null); }
 }
Esempio n. 11
0
        private void PlayPauseButton_Click(object sender, RoutedEventArgs e)
        {
            var sessionManager = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetAwaiter().GetResult();
            var currentSession = sessionManager.GetCurrentSession();

            if (currentSession != null)
            {
                currentSession.TryTogglePlayPauseAsync().GetAwaiter().GetResult();
            }
        }
Esempio n. 12
0
 /// <summary>
 /// This starts the MediaManager
 /// This can be changed to a constructor if you don't care for the first few 'new sources' events
 /// </summary>
 public static void Start()
 {
     if (!IsStarted)
     {
         var sessionManager = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetAwaiter().GetResult();
         SessionsChanged(sessionManager);
         sessionManager.SessionsChanged += SessionsChanged;
         IsStarted = true;
     }
 }
Esempio n. 13
0
        private async void AddCurrentSessionHandlers()
        {
            var sessionManager = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetAwaiter().GetResult();

            if (sessionManager != null && sessionManager.GetCurrentSession() != null)
            {
                sessionManager.GetCurrentSession().PlaybackInfoChanged    += MainPage_PlaybackInfoChanged; //Delegate to an instance method cannot have null 'this'
                sessionManager.GetCurrentSession().MediaPropertiesChanged += MainPage_MediaPropertiesChanged;
            }
        }
Esempio n. 14
0
        private async void SetupSMTCAsync()
        {
            GlobalSystemMediaTransportControlsSessionManager SMTC;

            SMTC = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();

            SMTC.SessionsChanged += SMTC_SessionsChanged;
            _SMTC = SMTC;

            LoadSessionControls();
        }
Esempio n. 15
0
        public static async Task <MediaSessionManager> CreateAsync()
        {
            SimpleLogger.DefaultLog("MediaSessionManager::CreateAsync called...");
            var instance = new MediaSessionManager {
                SystemSessionManager = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync(),
            };

            instance.SystemSessionManager.SessionsChanged += instance.OnSessionsChanged;
            SimpleLogger.DefaultLog("MediaSessionManager::CreateAsync DONE");
            return(instance);
        }
Esempio n. 16
0
        public override async void OnEnabled()
        {
            try
            {
                GSMTCSessionManager = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();

                GSMTCSessionManager.SessionsChanged += GSMTCSessionsChanged;

                LoadSessions();
            }
            catch { }
        }
Esempio n. 17
0
        public static void Stop()
        {
            if (globalSystemMediaTransportControlsSession != null)
            {
                globalSystemMediaTransportControlsSession.MediaPropertiesChanged -= GlobalSystemMediaTransportControlsSession_MediaPropertiesChanged;
            }

            globalSystemMediaTransportControlsSessionManager.CurrentSessionChanged -= GlobalSystemMediaTransportControlsSessionManager_CurrentSessionChanged;

            globalSystemMediaTransportControlsSessionManager = null;

            started = false;
        }
Esempio n. 18
0
        public TrackInfo GetTrackInfo()
        {
            var result = new TrackInfo();

            GlobalSystemMediaTransportControlsSessionMediaProperties currentTrack = null;

            currentTrack  = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetAwaiter().GetResult().GetCurrentSession().TryGetMediaPropertiesAsync().GetAwaiter().GetResult();
            result.Title  = currentTrack.Title ?? "Unknown Title";
            result.Artist = currentTrack.Artist ?? currentTrack.AlbumArtist ?? "Unknown Artist";
            result.Album  = currentTrack.AlbumTitle ?? currentTrack.AlbumTitle;

            return(result);
        }
Esempio n. 19
0
        public override void OnDisabled()
        {
            try
            {
                if (GSMTCSessionManager != null)
                {
                    GSMTCSessionManager.SessionsChanged -= GSMTCSessionsChanged;
                    GSMTCSessionManager = null;
                }

                ClearSessions();
            }
            catch { }
        }
Esempio n. 20
0
        private async Task WriteCurrentlyPlayingAsync()
        {
            var sessionManager = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetAwaiter().GetResult();

            if (sessionManager == null)
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    songAndArtist.Text      = " ";
                    playPauseButton.Content = Symbol.Play;
                });

                return;
            }
            var currentSession = sessionManager.GetCurrentSession();



            if (currentSession == null)
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    songAndArtist.Text      = " ";
                    playPauseButton.Content = '\uE102';
                });

                return;
            }



            var mediaProperties = currentSession.TryGetMediaPropertiesAsync().GetAwaiter().GetResult();
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                songAndArtist.Text = mediaProperties.Title + "\n\n" + mediaProperties.Artist;
                if (currentSession.GetPlaybackInfo().PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing)
                {
                    playPauseButton.Content = '\uE103';
                }
                else
                {
                    playPauseButton.Content = '\uE102';
                }
            });



            //var debug = currentSession.GetPlaybackInfo().PlaybackStatus;
        }
Esempio n. 21
0
        private static void SessionsChanged(GlobalSystemMediaTransportControlsSessionManager sender, SessionsChangedEventArgs args = null)
        {
            var sessionList = sender.GetSessions();

            foreach (var session in sessionList)
            {
                if (!CurrentMediaSessions.ContainsKey(session.SourceAppUserModelId))
                {
                    MediaSession mediaSession = new MediaSession(session);
                    CurrentMediaSessions[session.SourceAppUserModelId] = mediaSession;
                    OnNewSource?.Invoke(mediaSession);
                    mediaSession.OnSongChange(session);
                }
            }
        }
Esempio n. 22
0
        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;
        }
        public static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var sessions = (await GlobalSystemMediaTransportControlsSessionManager.RequestAsync()).GetSessions();

            foreach (var item in sessions)
            {
                var prop = await item.TryGetMediaPropertiesAsync();

                Console.WriteLine(prop.Title);
                Console.WriteLine(item.SourceAppUserModelId);
                //await item.TryTogglePlayPauseAsync();
            }
            //this project can get the control session of UWP APP

            Console.ReadKey();
        }
Esempio n. 24
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;
        }
Esempio n. 25
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);
        }
Esempio n. 26
0
 private async void SMTC_CurrentSessionChanged(GlobalSystemMediaTransportControlsSessionManager smtc, CurrentSessionChangedEventArgs args)
 {
     await Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(() =>
     {
         if (CurrentSession != null)
         {
             CurrentSession.MediaPropertiesChanged -= UpdateMediaProperties;
         }
         CurrentSession = smtc.GetCurrentSession();
         if (CurrentSession != null)
         {
             UpdateSessionInfo(CurrentSession);
             CurrentSession.MediaPropertiesChanged += UpdateMediaProperties;
             MainButton.Visibility = Visibility.Visible;
         }
         else
         {
             MainButton.Visibility = Visibility.Collapsed;
         }
     }));
 }
Esempio n. 27
0
        public void Destroy()
        {
            if (IsDestroyed)
            {
                return;
            }

            IsDestroyed = true;

            SystemSessionManager.SessionsChanged -= OnSessionsChanged;
            SystemSessionManager = null;

            foreach (var entry in CurrentSessions)
            {
                entry.Key.MediaPropertiesChanged -= OnMediaPropertiesChanged;
                entry.Key.PlaybackInfoChanged    -= OnPlaybackInfoChanged;
            }
            CurrentSessions.Clear();

            LastPlaybackData = null;
        }
Esempio n. 28
0
        static void Main(string[] args)
        {
            var gsmtcsm = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetAwaiter().GetResult().GetCurrentSession();

            if (gsmtcsm != null)
            {
                var mediaProperties = gsmtcsm.TryGetMediaPropertiesAsync().GetAwaiter().GetResult();

                var media = new Media
                {
                    Artist = mediaProperties.Artist,
                    Title  = mediaProperties.Title
                };
                var json = new JavaScriptSerializer().Serialize(media);
                Console.WriteLine(json);
            }
            else
            {
                Console.WriteLine("null");
            }
        }
Esempio n. 29
0
        private async void SMTC_SessionsChanged(GlobalSystemMediaTransportControlsSessionManager sender, SessionsChangedEventArgs args)
        {
            await Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(() =>
            {
                var sessions = SMTC.GetSessions();

                foreach (MusicSessionControl ses in SessionStackPanel.Children)
                {
                    ses.ClearSession();
                }
                SessionStackPanel.Children.Clear();

                foreach (var session in sessions)
                {
                    var newControl = new MusicSessionControl(session);
                    SessionStackPanel.Children.Add(newControl);
                }

                this.Height = sessions.Count * 60;
            }));
        }
Esempio n. 30
0
        private async Task <JObject> GetInfo()
        {
            var sessionManager = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();

            var currentSession = sessionManager.GetCurrentSession();
            GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties = null;

            byte[] bytes = null;
            if (currentSession == null)
            {
                _logger.Debug("Nothing is playing!");
            }
            else
            {
                mediaProperties = await currentSession.TryGetMediaPropertiesAsync();

                var previewStream = await mediaProperties.Thumbnail.OpenReadAsync();

                bytes = new byte[previewStream.Size];
                await previewStream.AsStream().ReadAsync(bytes, 0, (int)previewStream.Size);
            }

            var volume = await _defaultAudioDevice.GetVolumeAsync();

            var resultObj = new JObject {
                ["success"] = true,
                ["title"]   = mediaProperties?.Title,
                ["volume"]  = volume,
                ["muted"]   = _defaultAudioDevice.IsMuted,
                ["preview"] = bytes != null?Convert.ToBase64String(bytes) : null
            };

            _logger.Trace("Got result: {json}", resultObj);

            return(resultObj);
        }