Esempio n. 1
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. 2
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;
        }
Esempio n. 3
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;
            }
        }
Esempio n. 4
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. 5
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. 6
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. 7
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();
            }
        }
Esempio n. 8
0
        public static void update(object sender, ElapsedEventArgs arg)
        {
            GC.Collect();
            try
            {
                //Console.WriteLine("Updating...");
                var session = gsmtcsm.GetCurrentSession();
                var ses     = new List <GlobalSystemMediaTransportControlsSession>(gsmtcsm.GetSessions());
                var i       = 0;

                if (session != null)
                {
                    currentSession = session.SourceAppUserModelId.ToLower();

                    if (blockapp.GetCurrentParent().InvokeRequired)
                    {
                        blockapp.GetCurrentParent().Invoke(new Action(() =>
                        {
                            // Do anything you want with the control here
                            blockapp.Checked = Properties.Settings.Default.blockedApps.Contains(currentSession);
                            blockapp.Enabled = true;
                        }));
                    }
                    else
                    {
                        blockapp.Checked = Properties.Settings.Default.blockedApps.Contains(currentSession);
                        blockapp.Enabled = true;
                    }
                }
                else
                {
                    currentSession = null;
                    if (blockapp.GetCurrentParent().InvokeRequired)
                    {
                        blockapp.GetCurrentParent().Invoke(new Action(() =>
                        {
                            blockapp.Text    = "No media session";
                            blockapp.Checked = false;
                            blockapp.Enabled = false;
                        }));
                    }
                    else
                    {
                        blockapp.Text    = "No media session";
                        blockapp.Checked = false;
                        blockapp.Enabled = false;
                    }
                }

                if (session == null)
                {
                    trayIcon.Icon = trey;
                    trayIcon.Text = "SMTCRP - No media session";
                    if (isDisplaying)
                    {
                        isDisplaying = false;
                        client.ClearPresence();
                        Console.WriteLine("No media app open, Defering update cycle for 14 seconds...");
                        updTimer.Stop();
                        deferTimer.Start();
                    }
                    return;
                }
                else
                {
                    //TODO: make this nicer
                    if (Properties.Settings.Default.blockBrowsers)
                    {
                        while (session.SourceAppUserModelId.ToLower().StartsWith("discord") ||
                               session.SourceAppUserModelId.ToLower().StartsWith("firefox") ||
                               session.SourceAppUserModelId.ToLower().StartsWith("chrom") ||
                               session.SourceAppUserModelId.ToLower().StartsWith("opera") ||
                               session.SourceAppUserModelId.ToLower().StartsWith("msedge") ||
                               session.SourceAppUserModelId.ToLower().StartsWith("spotify"))
                        {
                            Console.WriteLine("browser_blocker {0}", session.SourceAppUserModelId);
                            if (session == null || i >= ses.Count)
                            {
                                session = null;
                                break;
                            }
                            session = ses[i];
                            i      += 1;
                        }
                    }
                }

                var    mediaProperties = session.TryGetMediaPropertiesAsync().GetAwaiter().GetResult();
                string appName         = session.SourceAppUserModelId.Replace(".exe", "");
                string appLogo         = "generic";
                if (icons.Contains(appName.ToLower()))
                {
                    appLogo = appName.ToLower();
                }
                if (appName.ToLower().StartsWith("winamp") || appName.ToLower().StartsWith("wacup"))
                {
                    appLogo = "winamp";
                    appName = "Winamp";
                }
                if (appName.EndsWith("ZuneMusic"))
                {
                    appLogo = "zunemusic";
                    appName = "Groove Music";
                }
                if (appLogo == "generic")
                {
                    Console.WriteLine("Using generic app icon! Contact theLMGN#4444 to get an app icon for {0}", appName);
                }
                if (appName.Contains("!")) // attempt to clean UWP app names
                {
                    appName = appName.Split(".").Last();
                }
                var data = mediaProperties.Title + " by " + mediaProperties.Artist + " via " + appName;
                if (blockapp.GetCurrentParent().InvokeRequired)
                {
                    blockapp.GetCurrentParent().Invoke(new Action(() =>
                    {
                        blockapp.Text = "Block " + appName;
                    }));
                }
                else
                {
                    blockapp.Text = "Block " + appName;
                }

                var pbi = session.GetPlaybackInfo();
                if (pbi.PlaybackStatus != GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing && !isDisplaying)
                {
                    return;
                }
                if (pbi.PlaybackStatus != GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing)
                {
                    trayIcon.Icon = trey;
                    trayIcon.Text = "SMTCRP - Nothing playing";
                    client.ClearPresence();
                    isDisplaying = false;
                    Console.WriteLine("Playback is {0}, Defering update cycle for 14 seconds...", pbi.PlaybackStatus);
                    updTimer.Stop();
                    deferTimer.Start();
                    return;
                }
                var tlp = session.GetTimelineProperties();

                if (data == lastData && isDisplaying)
                {
                    return;
                }
                Console.WriteLine(data);

                var count = mediaProperties.AlbumTrackCount;
                var num   = mediaProperties.TrackNumber;

                if (count < num)
                {
                    count = num;
                }

                if (Properties.Settings.Default.blockedApps.Contains(session.SourceAppUserModelId.ToLower()))
                {
                    Console.WriteLine("user_blocker {0}", session.SourceAppUserModelId);
                    trayIcon.Icon = troy;
                    trayIcon.Text = "SMTCRP - " + appName + " is blocked";
                    client.ClearPresence();
                    isDisplaying = false;
                    Console.WriteLine("Playback is {0}, Defering update cycle for 14 seconds...", pbi.PlaybackStatus);
                    updTimer.Stop();
                    deferTimer.Start();
                }
                else
                {
                    var generated_party = new Party()
                    {
                        ID   = Guid.NewGuid().ToString(),
                        Size = num,
                        Max  = count
                    };

                    if (!Properties.Settings.Default.useTrackNumbers)
                    {
                        generated_party = null;
                    }

                    var generated_details = "🎵 " + mediaProperties.Title;
                    if (Properties.Settings.Default.useAlbumTitle)
                    {
                        generated_details = "🎵 " + mediaProperties.AlbumTitle + " - " + mediaProperties.Title;
                    }

                    client.SetPresence(new RichPresence()
                    {
                        Details = generated_details,
                        State   = "👥 " + mediaProperties.Artist,
                        Party   = generated_party,
                        Assets  = new Assets()
                        {
                            LargeImageKey  = appLogo,
                            LargeImageText = "via " + appName,
                            SmallImageKey  = "logo",
                            SmallImageText = "https://github.com/thelmgn/smtcrp"
                        },
                    });
                    isDisplaying  = true;
                    lastData      = data;
                    trayIcon.Icon = tray;
                    if (data.Length > 54)
                    {
                        trayIcon.Text = "SMTCRP - " + data.Substring(0, 53) + "…";
                    }
                    else
                    {
                        trayIcon.Text = "SMTCRP - " + data;
                    }
                }
                Console.WriteLine("Defering update cycle for 14 seconds...");
                updTimer.Stop();
                deferTimer.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Esempio n. 9
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();
            }
        }