public GetVoiceClient ( ) : DiscordSharp.DiscordVoiceClient | ||
Результат | DiscordSharp.DiscordVoiceClient |
static Task ClientTask(DiscordClient client) { return Task.Run(() => { client.MessageReceived += (sender, e) => { if (e.message.content.StartsWith("?joinvoice")) { string[] split = e.message.content.Split(new char[] { ' ' }, 2); if (split[1] != "") { DiscordChannel toJoin = e.Channel.parent.channels.Find(x => (x.Name.ToLower() == split[1].ToLower()) && (x.Type == ChannelType.Voice)); if (toJoin != null) { client.ConnectToVoiceChannel(toJoin); } } } else if (e.message.content.StartsWith("?voice")) { string[] split = e.message.content.Split(new char[] { ' ' }, 2); if (File.Exists(split[1])) DoVoice(client.GetVoiceClient(), split[1]); } else if(e.message.content.StartsWith("?disconnect")) { client.DisconnectFromVoice(); } }; client.Connected += (sender, e) => { Console.WriteLine("Connected as " + e.user.Username); }; client.Connect(); }); }
private void SendVoice(string file, DiscordClient client, YouTubeVideo video) { DiscordVoiceClient vc = client.GetVoiceClient(); try { int ms = vc.VoiceConfig.FrameLengthMs; int channels = vc.VoiceConfig.Channels; int sampleRate = 48000; int blockSize = 48 * 2 * channels * ms; //sample rate * 2 * channels * milliseconds byte[] buffer = new byte[blockSize]; var outFormat = new WaveFormat(sampleRate, 16, channels); vc.SetSpeaking(true); if(video.AudioFormat == AudioFormat.Mp3) { using (var mp3Reader = new Mp3FileReader(video.Stream())) { using (var resampler = new MediaFoundationResampler(mp3Reader, outFormat) { ResamplerQuality = 60 }) { //resampler.ResamplerQuality = 60; int byteCount; while ((byteCount = resampler.Read(buffer, 0, blockSize)) > 0) { if (vc.Connected) { vc.SendVoice(buffer); } else break; } Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Voice finished enqueuing"); Console.ForegroundColor = ConsoleColor.White; resampler.Dispose(); mp3Reader.Close(); } } } else if(video.AudioFormat == AudioFormat.Vorbis) { using (var vorbis = new NAudio.Vorbis.VorbisWaveReader(video.Stream())) { using (var resampler = new MediaFoundationResampler(vorbis, outFormat) { ResamplerQuality = 60 }) { int byteCount; while ((byteCount = resampler.Read(buffer, 0, blockSize)) > 0) { if (vc.Connected) { vc.SendVoice(buffer); } else break; } Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Voice finished enqueuing"); Console.ForegroundColor = ConsoleColor.White; resampler.Dispose(); vorbis.Close(); } } } } catch (Exception ex) { try { MainEntry.owner.SendMessage("Exception during voice: `" + ex.Message + "`\n\n```" + ex.StackTrace + "\n```"); } catch { } } }