我在每个wave文件的末尾都听到了一声短促的声音。我的程序是一个从控制台窗口运行的OpenGL游戏引擎,this无关问题的答案建议在控制台应用程序中使用NAudio时使用函数回调,所以问题可能会在那里发送,我不知道。有什么想法吗?
我的用法是:
Audio.play("Music.wav");
音频课程:
public class Audio
{
private NAudio.Wave.BlockAlignReductionStream stream = null;
private NAudio.Wave.DirectSoundOut output = null;
public static void Play(string file)
{
Audio audio = new Audio();
audio.DisposeWave();
string fullPath = @"Audio/" + file;
if (fullPath.EndsWith(".mp3"))
{
NAudio.Wave.WaveStream pcm = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(fullPath));
audio.stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
}
else if (fullPath.EndsWith(".wav"))
{
NAudio.Wave.WaveStream pcm = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(fullPath));
audio.stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
}
else throw new InvalidOperationException("Not a correct audio file type.");
audio.output = new NAudio.Wave.DirectSoundOut();
audio.output.Init(audio.stream);
audio.output.Play();
}
private void DisposeWave()
{
if (output != null)
{
if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Stop();
output.Dispose();
output = null;
}
if (stream != null)
{
stream.Dispose();
stream = null;
}
}
}
}