Quantcast
Channel: naudio Work Item Rss Feed
Viewing all articles
Browse latest Browse all 738

Commented Issue: Streaming MP3-files, error decompressing last frame [16322]

$
0
0
Hi!

I'm trying to make a program that can stream audio from a server.
I all works fine, until i get to the last frame, then i get the following error:

Object reference not set to an instance of an object.

The code that returns the error is:
Array.Copy(frame.RawData, conversionStream.SourceBuffer, frame.FrameLength);

and is fund in "AcmMp3FrameDecompressor.cs" in "DecompressFrame"

I know that is happen because i stream a bit ahead to i can listen to the song directly from the stream.
But i don't know what i can do about it..
Here is the code i'm using to get frame from stream and decompress the frame.

IMp3FrameDecompressor decompressor = null;
try
{

var readFullyStream = new ReadFullyStream(responseStream);
do
{
if (bufferedWaveProvider != null && bufferedWaveProvider.BufferLength - bufferedWaveProvider.BufferedBytes < bufferedWaveProvider.WaveFormat.AverageBytesPerSecond / 4)
{
Console.WriteLine("Buffer getting full, taking a break");
Thread.Sleep(500);
}
else
{
Mp3Frame frame = null;
try
{
frame = Mp3Frame.LoadFromStream(readFullyStream);
}
catch (EndOfStreamException)
{
this.fullyDownloaded = true;
// reached the end of the MP3 file / stream
break;
}
catch (WebException)
{
// probably we have aborted download from the GUI thread
break;
}
if (decompressor == null)
{
// don't think these details matter too much - just help ACM select the right codec
// however, the buffered provider doesn't know what sample rate it is working at
// until we have a frame
WaveFormat waveFormat = new Mp3WaveFormat(frame.SampleRate, frame.ChannelMode == ChannelMode.Mono ? 1 : 2, frame.FrameLength, frame.BitRate);
decompressor = new AcmMp3FrameDecompressor(waveFormat);
this.bufferedWaveProvider = new BufferedWaveProvider(decompressor.OutputFormat);
this.bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(20); // allow us to get well ahead of ourselves
//this.bufferedWaveProvider.BufferedDuration = 250;
}
int decompressed = decompressor.DecompressFrame(frame, buffer, 0);
//Debug.WriteLine(String.Format("Decompressed a frame {0}", decompressed));
bufferedWaveProvider.AddSamples(buffer, 0, decompressed);

}

} while (playbackState != StreamingPlaybackState.Stopped);
Console.WriteLine("Exiting");
// was doing this in a finally block, but for some reason
// we are hanging on response stream .Dispose so never get there
decompressor.Dispose();
}

finally
{
if (decompressor != null)
{
decompressor.Dispose();
}
}

Hope this information helps :)
Comments: I've improved the exception to make the cause of this more obvious

Viewing all articles
Browse latest Browse all 738

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>