public async Task GetPlayUrlTest()
        {
            const int roomId  = 23058;
            var       playUrl = await BililiveApi.GetPlayUrlAsync(roomId);

            Assert.IsTrue(playUrl.StartsWith(@"https://"));
        }
示例#2
0
        public async Task HttpDownLoadTest()
        {
            const int roomId  = 23058;
            var       playUrl = BililiveApi.GetPlayUrlAsync(roomId).Result;

            Assert.IsTrue(playUrl.StartsWith(@"https://"));

            const string path = @"D:\Downloads\test.flv";

            var instance = new HttpDownLoad(playUrl, path, true);

#pragma warning disable 4014
            Task.Delay(10000).ContinueWith(task =>
#pragma warning restore 4014
            {
                instance.Dispose();
            });
            await instance.Start().ContinueWith(task =>
            {
                Console.WriteLine(task.Status);
                Console.WriteLine(task.IsCanceled);
                Console.WriteLine(task.IsCompleted);
                Console.WriteLine(task.IsCompletedSuccessfully);
                instance.Dispose();
            });
        }
示例#3
0
        public async Task <Room> FetchRoomInfoAsync()
        {
            var room = await BililiveApi.GetRoomInfoAsync(RoomId);

            RoomInfoUpdated?.Invoke(this, new RoomInfoUpdatedArgs {
                Room = room
            });
            return(room);
        }
        public async Task GetRoomInfoTest()
        {
            const int roomId = 3;
            var       room   = await BililiveApi.GetRoomInfoAsync(roomId);

            Assert.IsTrue(room.IsStreaming);
            Assert.AreEqual(room.RoomId, 23058);
            Assert.AreEqual(room.ShortRoomId, 3);
            Assert.AreEqual(room.UserName, @"3号直播间");
            Console.WriteLine(room.Title);
        }
示例#5
0
        public async Task Start()
        {
            while (true)
            {
                if (_disposedValue)
                {
                    return;
                }

                if (_currentRoom.IsRecording == RecordingStatus.Recording)
                {
                    LogEvent?.Invoke(this, new LogEventArgs {
                        Log = $@"[{_currentRoom.RoomId}] 已经在录制中了"
                    });
                    return;
                }

                EnsureDirectory();

                if (!_currentRoom.IsLive)
                {
                    return;
                }
                string url;
                try
                {
                    url = await BililiveApi.GetPlayUrlAsync(_currentRoom.RoomId);

                    url = await GetRedirectUrl(url);
                }
                catch (Exception ex)
                {
                    LogEvent?.Invoke(this, new LogEventArgs {
                        Log = $@"[{_currentRoom.RoomId}] {ex.Message}"
                    });
                    await ReCheck();

                    continue;
                }

                if (_response.StatusCode != HttpStatusCode.OK)
                {
                    LogEvent?.Invoke(this, new LogEventArgs {
                        Log = $@"尝试下载直播流时服务器返回了 ({_response.StatusCode}){_response.ReasonPhrase}"
                    });

                    await ReCheck();

                    continue;
                }

                var filename = FileName;
                _downLoadTask = new HttpDownLoad(url, filename, true);
                try
                {
                    _currentRoom.IsRecording = RecordingStatus.Recording;
                    LogEvent?.Invoke(this, new LogEventArgs {
                        Log = $@"[{_currentRoom.RoomId}] 开始录制"
                    });
                    await _downLoadTask.Start(_response)
                    .ContinueWith(task =>
                    {
                        LogEvent?.Invoke(this, new LogEventArgs {
                            Log = $@"[{_currentRoom.RoomId}] 录制结束"
                        });
                        RecordCompletedEvent?.Invoke(this, new LogEventArgs {
                            Log = filename
                        });
                    });
                }
                catch (Exception ex)
                {
                    LogEvent?.Invoke(this, new LogEventArgs {
                        Log = $@"[{_currentRoom.RoomId}] 录制出错:{ex}"
                    });
                }
                finally
                {
                    Stop();
                    await ReCheck();
                    await Start();
                }

                break;
            }
            Dispose();
        }