Pergunta

I would like to know how I can efficiently read local mp3 files with NAudio?
I tried several methods to read mp3 and play it back using a WaveOut but it is very slow sometimes.
For my current test case I took several mp3 files with filesizes between 80MB and 180MB.
First I used a Mp3FileReader:

myWaveOut.Init(new Mp3FileReader("test.mp3"));

Then I used a FileStream and passed it to an Mp3FileReader.

FileStream stream = File.OpenRead("test.mp3");
myWaveOut.Init(new Mp3FileReader(stream));

Same problems...
Further I used variatons of these (e.g. different buffer sizes/readers).

I inspected my taskmanager and everytime I read a file it seems that the reader reads the whole file before playing it. E.g. it took like 10-20 seconds to play one of my files. According to my taskmanager the file was loaded with 5.6 - 6.5 MB/s during this time. So in this time period the whole file could have been loaded.

Am I doing something wrong?
Are there more efficient ways to load larger files and stream them?

Foi útil?

Solução

It's because Mp3FileReader creates a Table of Contents up front allowing it to report the duration, and reposition accurately. The easiest way to avoid this is just use MediaFoundationReader to play MP3 files instead, or else, just make a very simple MP3FileReader class of your own, parsing out MP3Frames and decoding with AcmMp3FrameDecompressor. Maybe a future version of NAudio will make the TOC generation optional.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top