我按照NAudio网站上关于如何加载和播放mp3文件的教程,但即使我把音频文件放在正确的目录中,每当我运行时,程序崩溃“vshost32.exe已经停止工作”。有任何想法吗?我在Windows 7上使用Visual Studio 10.0。
这是教程给我的(确切)代码:
namespace NAudioTest
{
class Program
{
static IWavePlayer waveOutDevice;
static WaveStream mainOutputStream;
static WaveChannel32 volumeStream;
static void Main(string[] args)
{
waveOutDevice = new WaveOut();
mainOutputStream = CreateInputStream("Kalimba.mp3");
waveOutDevice.Init(mainOutputStream);
waveOutDevice.Play();
}
private static WaveStream CreateInputStream(string filename)
{
WaveChannel32 inputStream;
if (filename.EndsWith(".mp3"))
{
WaveStream mp3Reader = new Mp3FileReader(filename);
inputStream = new WaveChannel32(mp3Reader);
}
else
{
throw new InvalidOperationException("Unsupported extension");
}
volumeStream = inputStream;
return volumeStream;
}
}
}
(抱歉格式不佳)
答案 0 :(得分:0)
没关系,使用此代码让音频文件正常工作:
How to play a MP3 file using NAudio
class Program
{
static void Main()
{
using (var rdr = new Mp3FileReader(filename))
using (var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
{
waveOut.Play();
while (waveOut.PlaybackState == PlaybackState.Playing)
{
Thread.Sleep(100);
}
}
}
}
这样更好吗?虽然我不确定WaveOutEvent的去向。 谢谢你的帮助!!