0
votes

I am fairly new to C# and Mark's library NAudio. So I've tried learning by myself and I've come up with an basic audio player. But I have a problem.

When trying to load big files in the player the app freezes for 2-10 seconds while loading the entire file (I suppose). This is my code for reading the file:

if (target.EndsWith("mp3") || target.EndsWith("Mp3") || target.EndsWith("MP3"))
        {
            NAudio.Wave.WaveStream pcm = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(target));
            stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
        }

All I really want is to read the file in parts. Like a buffer. Read 10 seconds from the HDD to RAM memory, then after those 10 seconds run out, read the next 10 seconds, and so on. I think this should resolve the freeze issue I have with large files.

2
This is not the answer to your question but you can test for mp3 with if (target.Upper().EndsWith("MP3")).venerik
Please note you don't need WaveFormatConversionStream or BlockAlignReductionStream. Where did you see the code sample that includes these?Mark Heath
Hi! Than you for you answers. @MarkHeath - I've followed this tutorial series by Giawa - Youtube Video. The code is from this video, and the actual code I used it's shown @2:15.VladC
OK, that demo relates to NAudio 1.3. A lot has changed sinceMark Heath
Oh, I've might overlooked that bit. I'll search some newer tutorials, or I'll be trying to do a new script based on the examples provided from the codeplex site.VladC

2 Answers

0
votes

The cause of the delay is that Mp3FileReader creates a table of contents to allow it to determine the file length and to enable quicker repositioning. You could try using MediaFoundationReader instead which would be quicker, but won't work on Windows XP.

0
votes

all programs have delay to load big files. this is depended to client computer speed. but you can use backgroundWorker in your program and show a loading animation on your application Form during the file loading.

add backgroundWorker tool on your form

use this code on open button click:

backgroundWorker_name.RunWorkerAsync();

and put your code to the DoWork event

private void backgroundWorker_name_DoWork(object sender, DoWorkEventArgs e)
{
}