/// <summary> /// 获取直播间信息 /// </summary> /// <param name="UID">用户UID</param> /// <returns>直播间信息对象</returns> private LiveInfo GetRoomInfo(int UID) { // Json字符串 string str = FetchGetResponse("https://api.live.bilibili.com/room/v1/Room/getRoomInfoOld?mid=" + userInfo.UID.ToString()); LiveInfo liveInfo = new LiveInfo { OnAir = false, RoomTitle = "", RoomID = "" }; if (str != null) { // 直播状态 string liveStatus = str.Substring(str.IndexOf("\"liveStatus\":") + 13, 1); if (liveStatus == "1") { liveInfo.OnAir = true; } // 直播间名称 string RoomTitle = str.Substring(str.IndexOf("\"title\":\"") + 9); RoomTitle = RoomTitle.Substring(0, RoomTitle.IndexOf('"')); liveInfo.RoomTitle = RoomTitle; // 直播间ID string RoomID = str.Substring(str.IndexOf("\"roomid\":") + 9); RoomID = RoomID.Substring(0, RoomID.IndexOfAny(new char[] { ',', '}' })); liveInfo.RoomID = RoomID; } return(liveInfo); }
/// <summary> /// 监视函数 /// </summary> private void RoomMonitor() { // 每隔3s获取一次直播间状态, 若直播开始则下载 while (!Exit) { liveInfo = GetRoomInfo(userInfo.UID); this.Dispatcher.Invoke(SetRoomInfo); // 检测到正在直播执行下载 if (liveInfo.OnAir) { liveInfo.LiveVideoLink = GetDownloadLink(liveInfo.RoomID); this.Dispatcher.Invoke(SetRoomInfo); // 正确获取直播流地址 if (liveInfo.LiveVideoLink != null) { IsRecording = true; DownloadLive(); continue; } else { Thread.Sleep(30000); continue; } } Thread.Sleep(3000); } }