Pergunta

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.

Foi útil?

Solução

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.

Outras dicas

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)
{
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top