C# WebBrowser未更新到下一个目标URI(YouTube),卷控制

C# WebBrowser未更新到下一个目标URI(YouTube),卷控制,c#,youtube,webbrowser-control,volume,C#,Youtube,Webbrowser Control,Volume,我已经为Twitch开发了一个C#Bot有一段时间了,我一直在尝试让YouTube歌曲请求在不必使用网站之类的情况下进行。我在以下方面遇到了问题: 在第一首歌之后,它不会转到下一首歌 无法控制音量(需要)(可能通过WebBrowsercontrol或(Appcontrol) public void RequestSongCmd(字符串用户,字符串cmd){ 如果(!String.IsNullOrEmpty(cmd)和&!String.IsNullOrEmpty(用户)){ var-song=G

我已经为Twitch开发了一个C#Bot有一段时间了,我一直在尝试让YouTube歌曲请求在不必使用网站之类的情况下进行。我在以下方面遇到了问题:

  • 在第一首歌之后,它不会转到下一首歌
  • 无法控制音量(需要)(可能通过
    WebBrowser
    control或(
    App
    control)

  • public void RequestSongCmd(字符串用户,字符串cmd){
    如果(!String.IsNullOrEmpty(cmd)和&!String.IsNullOrEmpty(用户)){
    var-song=GetSong(cmd);
    var conf=new Config();
    int response=AddSong(歌曲);
    如果(响应==1){
    MessageHandler(conf.Nick,用户+”,歌曲\“+song.title+”\“(持续时间:+song.duration+”)添加到队列中”,8);
    user=song.requester;
    }否则如果(响应==0){
    MessageHandler(conf.Nick,user+”,歌曲太长。”,8);
    }else if(响应==-1){
    MessageHandler(conf.Nick,user+”,无效id.“,8);
    }
    }
    }
    公共无效停止歌曲(字符串usr){
    var db=新数据库();
    if(db.isMod(usr))StopSong();
    }
    公开课歌曲{
    公共字符串id{get;private set;}
    公共字符串标题{get;private set;}
    公共时间跨度持续时间{get;private set;}
    公共字符串请求程序{get;set;}
    公共歌曲(字符串id、字符串标题、时间跨度持续时间){
    this.id=id;
    this.title=标题;
    这个。持续时间=持续时间;
    this.requester=请求者;
    }
    }
    公共歌曲CurrentSong=null;
    公共int时间开始,估算;
    private System.Threading.Timer NextSongTimer=new System.Threading.Timer(NextSong,null,Timeout.Infinite,Timeout.Infinite);
    公共歌曲GetSong(字符串id){
    使用(WebClient w=新WebClient()){
    w、 Proxy=null;
    试一试{
    JObject json=JObject.Parse(w.DownloadString(“https://www.googleapis.com/youtube/v3/videos?id=“+id+”&key=AIzaSyAXkTQTVZmZo0Thi2bbIer9PZIvzMgdTsk&fields=items(id、代码段(标题)、状态(uploadStatus、privacyStatus)、内容详细信息(持续时间))&部分=代码段、内容详细信息、状态”);
    foreach(json[“items”]格式的JToken歌曲){
    如果(歌曲[“id”].ToString()==id){
    如果(歌曲[“状态”][“上传状态”].ToString()=“已处理”&(歌曲[“状态”][“私有状态”]).ToString()=“公共”| |歌曲[“状态”][“私有状态”]。ToString()=“未列出”)){
    返回新歌(id,歌曲[“snippet”][“title”].ToString(),System.Xml.XmlConvert.ToTimeSpan(歌曲[“contentDetails”][“duration”].ToString());
    }
    打破
    }
    }
    }抓住{
    }
    }
    返回null;
    }
    公共int添加歌曲(歌曲){
    如果(歌曲!=null){
    如果(song.duration.TotalSeconds=0;i--){
    歌曲。添加(老歌[i]);
    }
    }
    File.writeAllines(@“data\Songs.txt”,Songs.ToArray());
    }
    }
    公共void SkipSong(字符串用户){
    试一试{
    字符串名称=用户;
    数据库db=新数据库();
    var conf=new Config();
    if(db.isMod(name.ToLower())| | conf.BaseAdmin.ToLower()==user.ToLower()){
    MessageHandler(conf.Nick,user+“跳过了当前歌曲。”,8);
    ReorderQueue(true);
    播放歌曲();
    }
    }捕获{
    #如果你全部
    WriteLine(“跳过歌曲的错误有一致性”);
    #恩迪夫
    }
    }
    公共无效播放歌曲(歌曲歌曲=null){
    如果(歌曲==null){
    foreach(文件.ReadAllLines(@“data\Songs.txt”)中的字符串sSong){
    if(sSong!=“”&(song=GetSong(sSong.Split(“|”)[0])!=null){
    song.requester=sSong.Substring(sSong.LastIndexOf(“|”)+1);
    打破
    }
    }
    }
    如果(歌曲!=null){
    当前歌曲=歌曲;
    this.Invoke(新方法调用器(委托{
    this.SongRequest.Navigate(新Uri(“https://www.youtube.com/apiplayer?video_id=“+song.id
    +“&version=3&autoplay=1&enablejsapi=1&feature=player\u embedded&controls=0&modestbranding=1&rel=0&showinfo=0&autohide=1&color=white&playerapiid=musicPlayer&iv\u load\u policy=3”);
    }));
    }
    }
    公共静态void NextSong(对象状态){
    var ts=新的MainForm();
    t.ReorderQueue();
    t.PlaySong();
    }
    已导航公共无效SongRequestPlayer_(对象发送方,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(newdatetime(1970,1,1))).TotalSeconds;
    }
    公共电台停止播放歌曲(){
    TimeStarted=EstimatedEnding=0;
    var form=新的MainForm();
    NextSongTimer.Change(Timeout.Infinite,Timeout.Infinite);
    
            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;
            }
        }