I’m using nAudio to play a background song in my WPF Application.
Now to start a new instace of a song the code to is this:
private 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;
}
Which works fine, but only to a specific file path.
Now if i try sending a Pack URL to this function, i get error that the format isn’t supported.
Anyone ever encouterd this and knows what i should do?
SOLVED :
thanks to minitech answer, this code eventully worked :
StreamResourceInfo resource = Application.GetResourceStream(
new Uri("YearBook;component/Resources/Music/1.mp3", UriKind.Relative));
mainOutputStream = CreateInputStream(resource.Stream);
waveOutDevice.Init(mainOutputStream);
private WaveStream CreateInputStream(Stream fileName)
{
WaveChannel32 inputStream;
WaveStream mp3Reader = new Mp3FileReader(fileName);
inputStream = new WaveChannel32(mp3Reader);
volumeStream = inputStream;
return volumeStream;
}
The
Mp3FileReaderconstructor is overloaded to take a stream. I don’t know how WPF resources work, but if you can get your file as aStreamobject somehow, then you can use that. Here’s how you can play abyte[]: