WebBrowser does not update url for next target URI (YouTube), volume control

I have been working on C # Bot for Twitch for a while and I was trying to get YouTube song requests without using websites etc. I had a problem with this:

  • After the first song, he does not go to the next song
  • Can't control volume (what is needed) (perhaps with a control WebBrowser

    or ( App

    control)?

        public void RequestSongCmd(string user, string cmd) {
            if (!String.IsNullOrEmpty(cmd) && !String.IsNullOrEmpty(user)) {
                var song = GetSong(cmd);
                var conf = new Config();
                int response = AddSong(song);
                if (response == 1) {
                    MessageHandler(conf.Nick, user + ", song \"" + song.title + "\" (duration: " + song.duration + ") added to queue", 8);
                    user = song.requester;
                } else if (response == 0) {
                    MessageHandler(conf.Nick, user + ", song is too long.", 8);
                } else if (response == -1) {
                    MessageHandler(conf.Nick, user + ", invalid id.", 8);
                }
            }
        }
        public void StopSong(string usr) {
            var db = new Database();
            if (db.isMod(usr)) StopSong();
        }
        public class Song {
            public string id { get; private set; }
            public string title { get; private set; }
            public TimeSpan duration { get; private set; }
            public string requester { get; set; }
            public Song(string id, string title, TimeSpan duration) {
                this.id = id;
                this.title = title;
                this.duration = duration;
                this.requester = requester;
            }
        }
        public Song CurrentSong = null;
        public int TimeStarted, EstimatedEnding;
        private System.Threading.Timer NextSongTimer = new System.Threading.Timer(NextSong, null, Timeout.Infinite, Timeout.Infinite);
        public Song GetSong(string id) {
            using (WebClient w = new WebClient()) {
                w.Proxy = null;
                try {
                    JObject json = JObject.Parse(w.DownloadString("https://www.googleapis.com/youtube/v3/videos?id=" + id + "&key=AIzaSyAXkTQTVZmZo0Thi2bbIer9PZIvzMgdTsk&fields=items(id,snippet(title),status(uploadStatus,privacyStatus),contentDetails(duration))&part=snippet,contentDetails,status"));
                    foreach (JToken song in json["items"]) {
                        if (song["id"].ToString() == id) {
                            if (song["status"]["uploadStatus"].ToString() == "processed" && (song["status"]["privacyStatus"].ToString() == "public" || song["status"]["privacyStatus"].ToString() == "unlisted")) {
                                return new Song(id, song["snippet"]["title"].ToString(), System.Xml.XmlConvert.ToTimeSpan(song["contentDetails"]["duration"].ToString()));
                            }
                            break;
                        }
                    }
                } catch {
                }
            }
            return null;
        }
        public int AddSong(Song song) {
            if (song != null) {
                if (song.duration.TotalSeconds <= 900) {
                    List<string> songs = new List<string>();
                    if (File.Exists(@"data\Songs.txt")) {
                        songs = File.ReadAllLines(@"data\Songs.txt").ToList();
                    }
                    songs.Add(song.id + "|" + song.title + "|" + song.requester);
                    File.WriteAllLines(@"data\Songs.txt", songs.ToArray());
                    return 1;
                }
                return 0;
            }
            return -1;
        }
        public void ReorderQueue(bool Remove = false, int start = 1) {
            if (File.Exists(@"data\Songs.txt")) {
                List<string> OldSongs = File.ReadAllLines(@"data\Songs.txt").ToList(), songs = new List<string>();
                for (int i = start; i < OldSongs.Count; i++) {
                    songs.Add(OldSongs[i]);
                }
                if (!Remove) {
                    for (int i = start - 1; i >= 0; i--) {
                        songs.Add(OldSongs[i]);
                    }
                }
                File.WriteAllLines(@"data\Songs.txt", songs.ToArray());
            }
        }
        public void SkipSong(string user) {
                try {
                    string name = user;
                    Database db = new Database();
                    var conf = new Config();
                    if (db.isMod(name.ToLower()) || conf.BaseAdmin.ToLower() == user.ToLower()) {
                        MessageHandler(conf.Nick, user + " has skipped the current song.", 8);
                        ReorderQueue(true);
                        PlaySong();
                    }
                } catch { 
                    #if DEBUG_ALL
                    Debug.WriteLine("Error has accord in skipping the song");
                    #endif
                }
        }
        public void PlaySong(Song song = null) {
            if (song == null) {
                foreach (string sSong in File.ReadAllLines(@"data\Songs.txt")) {
                    if (sSong != "" && (song = GetSong(sSong.Split('|')[0])) != null) {
                        song.requester = sSong.Substring(sSong.LastIndexOf('|') + 1);
                        break;
                    }
                }
            }
            if (song != null) {
                    CurrentSong = song;
                    this.Invoke(new MethodInvoker(delegate {
                        this.SongRequest.Navigate(new Uri("https://www.youtube.com/apiplayer?video_id=" + song.id
                            + "&version=3&autoplay=1&enablejsapi=1&feature=player_embedded&controls=0&modestbranding=1&rel=0&showinfo=0&autohide=1&color=white&playerapiid=musicPlayer&iv_load_policy=3"));
                    }));
            }
        }
        public static void NextSong(object state) {
            var ts = new MainForm();
            ts.ReorderQueue();
            ts.PlaySong();
        }
        public void SongRequestPlayer_Navigated(object sender, System.Windows.Forms.WebBrowserNavigatedEventArgs e) {
            EstimatedEnding = (TimeStarted = GetUnixTimeNow()) + (int)CurrentSong.duration.TotalSeconds + 1;
            NextSongTimer.Change((int)CurrentSong.duration.TotalSeconds * 1000 + 1000, Timeout.Infinite);
        }
        public int GetUnixTimeNow() {
            return (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
        }
        public void StopSong() {
            TimeStarted = EstimatedEnding = 0;
            var form = new MainForm();
            NextSongTimer.Change(Timeout.Infinite, Timeout.Infinite);
            form.SongRequest.Url = null;
            CurrentSong = null;
        }
    }

      


I was under the assumption that the best way to handle the volume in the WebBrowser control is to handle the entire volume of the application, is that true?

And also, for the next song, what's going on there? It has to work, it is used to work. I have no idea what I have been doing wrong with this goal for a while.

* I must point out: I am using WinForms

for the whole project, so threading

it is not a problem because I have already tried various methods to get this to work. As for the IRC library, I am using SmartIRC4Net

. *

UPDATE: Fixed several issues in the code, still can't figure out what to do next with the next song,

+3


source to share





All Articles