【发布时间】:2019-01-07 18:20:05
【问题描述】:
我想在语音频道中播放 mp3 文件。 BOT成功可以连接,但不播放任何东西,并抛出异常。
代码
public async Task SendAudioAsync(IGuild guild, IMessageChannel channel, string path)
{
try
{
if (!File.Exists(path))
{
await channel.SendMessageAsync("File does not exist.");
return;
}
IAudioClient client;
if (ConnectedChannels.TryGetValue(guild.Id, out client))
{
await Log.d($"Starting playback of {path} in {guild.Name}", src);
using (var reader = new Mp3FileReader(path))
using (var output = client.CreateOpusStream())
{
try
{
reader.CopyTo(output);//<- AudioService.cs, line: 70
}
catch(Exception e)
{
await Log.e(e.ToString(), src);
}
finally { await output.FlushAsync(); }
}
}
}
catch (Exception e)
{
await Log.e(e.ToString(), src);
}
}
异常
System.ArgumentException: Shift and length outside the array boundaries or the number of elements is greater than the number of items in the source collection from the index to the end of the collection.
System.Buffer.BlockCopy(Array src, Int32 srcOffset , Array dst, Int32 dstOffset, Int32 count)
at Discord.Audio.Streams.BufferedWriteStream.<WriteAsync>d__21.MoveNext()
--- Trigger end tracking from the previous occurrence of the exception ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Discord.Audio.AudioStream.Write(Byte [] buffer, Int32 offset, Int32 count)
at System.IO.Stream.InternalCopyTo(Stream destination, Int32 <SendAudioAsync> d__4.MoveNext() location: C:\Users\noel\source\repos\DiscordBot\Bot\Core\Voice\AudioService.cs, line: 70
这个版本的异常,用谷歌翻译从匈牙利语翻译过来..
更新
我做了更多研究,发现this example,因为它已经过时了我重写了一点,现在看起来像这样:
public async Task SendAudioAsync(IGuild guild, IMessageChannel channel, string path)
{
try
{
if (!File.Exists(path))
{
await channel.SendMessageAsync("File does not exist.");
return;
}
IAudioClient client;
if (ConnectedChannels.TryGetValue(guild.Id, out client))
{
await Log.d($"Starting playback of \"{path}\" in \"{guild.Name}\"", src);
var OutFormat = new WaveFormat(48000, 16, 2);
using (var MP3Reader = new Mp3FileReader(path)) // Create a new Disposable MP3FileReader, to read audio from the filePath parameter
using (var resampler = new MediaFoundationResampler(MP3Reader, OutFormat)) // Create a Disposable Resampler, which will convert the read MP3 data to PCM, using our Output Format
{
resampler.ResamplerQuality = 60; // Set the quality of the resampler to 60, the highest quality
int blockSize = OutFormat.AverageBytesPerSecond / 50; // Establish the size of our AudioBuffer
byte[] buffer = new byte[blockSize];
int byteCount;
while ((byteCount = resampler.Read(buffer, 0, blockSize)) > 0) // Read audio into our buffer, and keep a loop open while data is present
{
if (byteCount < blockSize)
{
// Incomplete Frame
for (int i = byteCount; i < blockSize; i++)
buffer[i] = 0;
}
using(var output = client.CreatePCMStream(AudioApplication.Mixed))
await output.WriteAsync(buffer, 0, blockSize); // Send the buffer to Discord
}
}
}
}
catch (Exception e)
{
await Log.e(e.ToString(), src);
}
}
现在它什么都不做,也不抛出任何异常。
【问题讨论】:
标签: c# naudio discord.net