Playing a never-ending stream will never delete used content from memory. #936
-
Hey there, asking for advice here. I'm using NAudio to play a never-ending stream from a radio station. See the below code for some context. This works perfectly fine - it will do just as intended - play the never-ending stream, from the given URL. The only problem I have is deleting the content I've played from memory. The memory usage of the application will just steadily increase. I don't really know much about So, how would I go about optimizing this, to remove the used playback components from memory? I've tested this code, and the size of the Ideally, the simple option sounds like it is just going to be to dump the buffer/stream/whatever it is after it reaches a certain size. How would I go about doing this? Any help would be appreciated 🙏 Kind regards, ChangePlaybackState(RadioPlaybackState.Initializing);
ms.Close();
ms = new MemoryStream();
thread = new Thread(delegate (object o)
{
var response = WebRequest.Create(url).GetResponse();
using (var stream = response.GetResponseStream())
{
byte[] buffer = new byte[65536]; // 64kb chunks
int read;
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
var pos = ms.Position;
ms.Position = ms.Length;
ms.Write(buffer, 0, read);
ms.Position = pos;
}
}
});
thread.Start();
// Pre-buffering some data to allow NAudio to start playing
while (ms.Length < PrebufferSize)
{
ChangePlaybackState(RadioPlaybackState.Prebuffering);
Thread.Sleep(500);
}
ChangePlaybackState(RadioPlaybackState.Playing);
ms.Position = 0;
Debug.WriteLine("Using decoder: {0}", (DecodeEngine)User.Default.Decoder);
using (WaveStream blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(new Mp3FileReader(ms))))
{
using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
{
waveOut.Init(blockAlignedStream);
waveOut.Play();
while (waveOut.PlaybackState == NAudio.Wave.PlaybackState.Playing)
{
Thread.Sleep(100);
waveOut.Volume = this.volume;
if (StopNextCycle)
break;
}
}
}
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Take a look at the MP3 Streaming Demo code for a better approach to achieving what you are trying to do |
Beta Was this translation helpful? Give feedback.
Take a look at the MP3 Streaming Demo code for a better approach to achieving what you are trying to do