Пример #1
0
            private async Task StartCurrentTrackPlayback()
            {
                if (IsPlaying) return;

                if (PlaybackChannel == null)
                {
                    await
                        ChatChannel.SafeSendMessage(
                            "Playback channel wasn't set when attempting to start track playback.");
                    return;
                }

                if (_voiceClient == null)
                {
                    await
                        ChatChannel.SafeSendMessage("Voice client wasn't set when attempting to start track playback.");
                    return;
                }

                _stopTrackFlag = false;
                IsPlaying = true;

                using (AudioStreamer streamer = new AudioStreamer(await CurrentTrack.GetStream(), _client))
                {
                    if (_skipToFlag)
                    {
                        streamer.Start(_skipTime);
                        _skipTime = TimeSpan.Zero;
                        _skipToFlag = false;
                    }
                    else
                    {
                        await PrintCurrentTrack();
                        streamer.Start();
                    }

                    int bufferSize = 1920*_client.Audio().Config.Channels;
                    byte[] buffer = new byte[bufferSize];

                    // Wait for the ffmpeg stream to become available.
                    while (streamer.OutputStream == null) await Task.Delay(10);

                    while (true)
                    {
                        if (_stopTrackFlag)
                        {
                            _pausePlaybackFlag = false;
                            _stopTrackFlag = false;
                            break;
                        }
                        if (_pausePlaybackFlag)
                            await Task.Delay(100);
                        else
                        {
                            if (streamer.OutputStream.ReadExactly(buffer, bufferSize))
                                break;
                            _voiceClient.Send(buffer, 0, bufferSize);
                        }
                    }
                }
                IsPlaying = false;
            }
Пример #2
0
            private async Task StartCurrentTrackPlayback()
            {
                if (IsPlaying)
                {
                    return;
                }

                if (PlaybackChannel == null)
                {
                    await
                    ChatChannel.SafeSendMessage(
                        "Playback channel wasn't set when attempting to start track playback.");

                    return;
                }

                if (_voiceClient == null)
                {
                    await
                    ChatChannel.SafeSendMessage("Voice client wasn't set when attempting to start track playback.");

                    return;
                }

                _stopTrackFlag = false;
                IsPlaying      = true;

                using (AudioStreamer streamer = new AudioStreamer(await CurrentTrack.GetStream(), _client))
                {
                    if (_skipToFlag)
                    {
                        streamer.Start(_skipTime);
                        _skipTime   = TimeSpan.Zero;
                        _skipToFlag = false;
                    }
                    else
                    {
                        await PrintCurrentTrack();

                        streamer.Start();
                    }

                    int    bufferSize = 1920 * _client.Audio().Config.Channels;
                    byte[] buffer     = new byte[bufferSize];

                    // Wait for the ffmpeg stream to become available.
                    while (streamer.OutputStream == null)
                    {
                        await Task.Delay(10);
                    }

                    while (true)
                    {
                        if (_stopTrackFlag)
                        {
                            _pausePlaybackFlag = false;
                            _stopTrackFlag     = false;
                            break;
                        }
                        if (_pausePlaybackFlag)
                        {
                            await Task.Delay(100);
                        }
                        else
                        {
                            if (streamer.OutputStream.ReadExactly(buffer, bufferSize))
                            {
                                break;
                            }
                            _voiceClient.Send(buffer, 0, bufferSize);
                        }
                    }
                }
                IsPlaying = false;
            }