Пример #1
0
        void stateCheckTimer_Tick(object sender, EventArgs e)
        {
            // figure out what spotify is doing right now

            var iframes = browser.Document.GetElementsByTagName("iframe");

            foreach (Gecko.DOM.GeckoIFrameElement frame in iframes)
            {
                GeckoHtmlElement doc = null;

                string queryArtist = "", queryTrack = "", queryTrackUri = "", queryIsPlaying = "", queryAddButton = "", attrTrackUri = "";

                // Old Player
                if (frame.Id == "app-player")
                {
                    doc = frame.ContentDocument.DocumentElement as GeckoHtmlElement;

                    queryIsPlaying = "#play-pause.playing";
                    queryArtist    = "#track-artist a";
                    queryTrack     = "#track-name a";
                    queryTrackUri  = "#track-name a";
                    attrTrackUri   = "href";
                    queryAddButton = "#track-add";
                }
                // New Player
                if (frame.Id == "main")
                {
                    doc = frame.ContentDocument.DocumentElement as GeckoHtmlElement;

                    queryIsPlaying = "#play.playing";
                    queryArtist    = "p.artist a";
                    queryTrack     = "p.track a";
                    queryTrackUri  = "p.track a";
                    attrTrackUri   = "data-uri";
                    queryAddButton = ".caption button.button-add";
                }

                if (doc != null)
                {
                    SpotifyState oldState = currentSpotifyState;
                    string       oldTrack = currentTrack.TrackUri;

                    // get current track
                    var isPlaying = doc.QuerySelector(queryIsPlaying);
                    if (isPlaying != null)
                    {
                        currentSpotifyState = SpotifyState.Playing;
                        var artist   = doc.QuerySelector(queryArtist).TextContent;
                        var title    = doc.QuerySelector(queryTrack).TextContent;
                        var trackUri = doc.QuerySelector(queryTrackUri).GetAttribute(attrTrackUri);
                        currentTrack = new Mp3Tag(title, artist, trackUri);
                    }
                    else
                    {
                        currentSpotifyState = SpotifyState.Paused;
                    }

                    // ad detection (new player only)
                    var addToMyMusicButton = doc.QuerySelector(queryAddButton);
                    if (addToMyMusicButton != null)
                    {
                        var style = addToMyMusicButton.Attributes["style"];
                        if (style != null)
                        {
                            if (style.NodeValue.Contains("display: none"))
                            {
                                currentSpotifyState = SpotifyState.Ad;
                            }
                        }
                    }

                    // extra ad detection, works for old player too
                    if (currentTrack.Artist == "Spotify")
                    {
                        currentSpotifyState = SpotifyState.Ad;
                    }


                    // mute sound on ads
                    if (currentSpotifyState == SpotifyState.Ad)
                    {
                        if (MuteOnAdsCheckBox.Checked && !MutedSound)
                        {
                            addToLog("Ads detected - Attempting to Mute!");
                            Util.ToggleMuteVolume(this.Handle);
                            MutedSound = true;
                        }
                    }
                    else
                    {
                        if (MutedSound)
                        {
                            addToLog("Un-Muting");
                            Util.ToggleMuteVolume(this.Handle);
                            MutedSound = false;
                        }
                    }

                    // set state if changed
                    if (oldState != currentSpotifyState || oldTrack != currentTrack.TrackUri)
                    {
                        if (currentSpotifyState == SpotifyState.Playing)
                        {
                            var song = currentTrack.Artist + " - " + currentTrack.Title;
                            songLabel.Text = song;
                            addToLog("Now playing: " + song + " (" + currentTrack.TrackUri + ")");
                            if (_currentApplicationState != RecorderState.NotRecording && oldTrack != currentTrack.TrackUri)
                            {
                                ChangeApplicationState(RecorderState.Recording);
                            }
                            else if (_currentApplicationState == RecorderState.Recording)
                            {
                                ChangeApplicationState(RecorderState.WaitingForRecording);
                            }
                        }
                        else if (currentSpotifyState == SpotifyState.Paused)
                        {
                            addToLog("Music stopped");
                            if (_currentApplicationState == RecorderState.Recording)
                            {
                                ChangeApplicationState(RecorderState.WaitingForRecording);
                            }
                        }
                        else if (currentSpotifyState == SpotifyState.Ad)
                        {
                            addToLog("Ads...");
                            if (_currentApplicationState == RecorderState.Recording)
                            {
                                ChangeApplicationState(RecorderState.WaitingForRecording);
                            }
                        }
                    }

                    break;
                }
            }

            songLabel.Visible = _currentApplicationState == RecorderState.Recording;
        }