コード例 #1
0
ファイル: StreamRequest.cs プロジェクト: Cantinhu/BotTest
        internal async Task StartPlayback(DiscordClient c)
        {
            Console.WriteLine("Starting playback.");
            if (State == StreamState.Playing)
            {
                return;
            }
            State = StreamState.Playing;
            if (parent.OnBuffering != null)
            {
                parent.OnBuffering();
            }

            Task.Factory.StartNew(async() => {
                await BufferSong();
            }).ConfigureAwait(false);

            // prebuffering wait stuff start
            int bufferAttempts = 0;
            int waitPerAttempt = 500;

            while (!prebufferingComplete && bufferAttempts++ < 15)
            {
                await Task.Delay(waitPerAttempt);
            }
            if (prebufferingComplete)
            {
                Console.WriteLine($"Prebuffering finished in {bufferAttempts*500}");
            }
            // prebuffering wait stuff end

            if (buffer.Length > 0)
            {
                Console.WriteLine("Prebuffering complete.");
            }
            else
            {
                Console.WriteLine("Nothing was buffered, try another song and check your GoogleApikey.");
            }

            int blockSize = 1920 * c.Services.Get <AudioService>().Config.Channels;

            byte[] voiceBuffer = new byte[blockSize];

            if (parent.OnStarted != null)
            {
                parent.OnStarted();
            }

            int attempt = 0;

            while (!IsCanceled)
            {
                int readCount = 0;
                //adjust volume

                lock (_bufferLock) {
                    readCount = buffer.Read(voiceBuffer, 0, voiceBuffer.Length);
                }

                if (readCount == 0)
                {
                    if (attempt == 4)
                    {
                        Console.WriteLine($"Failed to read {attempt} times. Breaking out. [{DateTime.Now.Second}]");
                        break;
                    }
                    else
                    {
                        ++attempt;
                        await Task.Delay(15);
                    }
                }
                else
                {
                    attempt = 0;
                }

                if (State == StreamState.Completed)
                {
                    Console.WriteLine("Canceled");
                    break;
                }
                voiceBuffer = adjustVolume(voiceBuffer, parent.Volume);
                // parent.MusicControls.Voice.Send(voiceBuffer, 0, voiceBuffer.Length);

                //  parent.MusicControls.VoiceClient.VoiceSocket.SendPCMFrames(voiceBuffer, 0, voiceBuffer.Length);
                //  parent.MusicControls.VoiceClient.OutputStream.Write(voiceBuffer, 0, voiceBuffer.Length);
                parent.MusicControls.VoiceClient.Send(voiceBuffer, 0, voiceBuffer.Length);


                while (IsPaused)
                {
                    await Task.Delay(50);
                }
            }
            parent.MusicControls.VoiceClient.Wait();
            Stop();
        }
コード例 #2
0
ファイル: StreamRequest.cs プロジェクト: Dekryptor/NadekoBot
        internal async Task StartPlayback()
        {
            Console.WriteLine("Starting playback.");
            if (State == StreamState.Playing)
            {
                return;
            }
            State = StreamState.Playing;
            if (parent.OnBuffering != null)
            {
                parent.OnBuffering();
            }

            Task.Run(async() => {
                await BufferSong();
            }).ConfigureAwait(false);

            // prebuffering wait stuff start
            int bufferAttempts = 0;
            int waitPerAttempt = 500;
            int toAttemptTimes = parent.RadioLink ? 4 : 8;

            while (!prebufferingComplete && bufferAttempts++ < toAttemptTimes)
            {
                await Task.Delay(waitPerAttempt);
            }
            if (prebufferingComplete)
            {
                Console.WriteLine($"Prebuffering finished in {bufferAttempts * 500}");
            }
            // prebuffering wait stuff end

            if (buffer.Length > 0)
            {
                Console.WriteLine("Prebuffering complete.");
            }
            else
            {
                Console.WriteLine("Nothing was buffered, try another song and check your GoogleApikey.");
            }

            int blockSize = 1920 * NadekoBot.client.GetService <AudioService>()?.Config?.Channels ?? 3840;

            byte[] voiceBuffer = new byte[blockSize];

            if (parent.OnStarted != null)
            {
                parent.OnStarted();
            }

            int attempt = 0;

            while (!IsCanceled)
            {
                int readCount = 0;
                //adjust volume

                lock (_bufferLock) {
                    readCount = buffer.Read(voiceBuffer, 0, blockSize);
                }

                if (readCount == 0)
                {
                    if (attempt == 4)
                    {
                        Console.WriteLine($"Failed to read {attempt} times. Breaking out.");
                        break;
                    }
                    else
                    {
                        ++attempt;
                        await Task.Delay(15);
                    }
                }
                else
                {
                    attempt = 0;
                }

                if (State == StreamState.Completed)
                {
                    Console.WriteLine("Canceled");
                    break;
                }
                voiceBuffer = adjustVolume(voiceBuffer, parent.Volume);
                parent.MusicControls.VoiceClient.Send(voiceBuffer, 0, readCount);

                while (IsPaused)
                {
                    await Task.Delay(100);
                }
            }
            parent.MusicControls.VoiceClient.Wait();
            Stop();
        }