I am trying to play an mp3 from an internet source while streaming in the same time. I've writen the code below and it throws the exception WaveBadFormat calling WaveOut
at the player.Init()
line. How should I fix this?
string testLink = "http://users.skynet.be/fa046054/home/P22/track49.mp3";
req = WebRequest.Create(testLink) as HttpWebRequest;
resp = req.GetResponse() as HttpWebResponse;
str = resp.GetResponseStream();
int readBytes = 0;
int totalBytes = 0;
byte[] buffer = new byte[1024];
byte[] mp3Buffer = new byte[45000];
IMp3FrameDecompressor decompressor = null;
BufferedWaveProvider provider = null;
WaveOut player = new WaveOut();
Mp3Frame frame = Mp3Frame.LoadFromStream(new ReadFullyStream(str));
while ((readBytes = str.Read(buffer, 0, buffer.Length)) > 0)
{
totalBytes += readBytes;
if (decompressor == null && totalBytes > 1024)
{
Mp3WaveFormat format = new Mp3WaveFormat(frame.SampleRate,
frame.ChannelMode == ChannelMode.Mono ? 1 : 2,
frame.FrameLength, frame.BitRate);
decompressor = new AcmMp3FrameDecompressor(format);
provider = new BufferedWaveProvider(format);
int x = decompressor.DecompressFrame(frame, mp3Buffer, 0);
provider.AddSamples(mp3Buffer, 0, x);
player.Init(provider);
player.Play();
continue;
}
Application.DoEvents();
if (decompressor == null)
continue;
int decNum = decompressor.DecompressFrame(frame, mp3Buffer, 0);
provider.AddSamples(mp3Buffer, 0, decNum);
}