NAudio Mp3 Playback in Console
- by Kurru
Hi
I'm trying to make a helper dll that will simplify the NAudio framework into a subset of functions I'm likely to need but I've hit a stumbling block right off the bat.
I'm trying to use the following code to play an mp3 but I'm not hearing anything at all.
Any help would be appreciated!
static WaveOut waveout;
static WaveStream playback;
static System.Threading.ManualResetEvent wait = new System.Threading.ManualResetEvent(false);
static void Main(string[] args)
{
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(PlaySong));
t.Start();
wait.WaitOne();
System.Threading.Thread.Sleep(2 * 1000);
waveout.Stop();
waveout.Dispose();
playback.Dispose();
}
static void PlaySong()
{
waveout = new WaveOut();
playback = OpenMp3Stream(@"songname.mp3");
waveout.Init(playback);
waveout.Play();
Console.WriteLine("Started");
wait.Set();
}
private static WaveChannel32 OpenMp3Stream(string fileName)
{
WaveChannel32 inputStream;
WaveStream mp3Reader = new Mp3FileReader(fileName);
WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(mp3Reader);
WaveStream blockAlignedStream = new BlockAlignReductionStream(pcmStream);
inputStream = new WaveChannel32(blockAlignedStream);
return inputStream;
}