0

I'm trying to trim a mp3 file. using this code:

private void TrimMp3(string open, string save)
    {
        using (var mp3FileReader = new Mp3FileReader(open))
        using (var writer = File.Create(save))
        {
            var startPostion = TimeSpan.FromSeconds(60);
            var endPostion = TimeSpan.FromSeconds(90);
            mp3FileReader.CurrentTime = startPostion;
            while (mp3FileReader.CurrentTime < endPostion)
            {
                var frame = mp3FileReader.ReadNextFrame();
                if (frame == null) break;
                writer.Write(frame.RawData, 0, frame.RawData.Length);
            }
        }
    }

"open" is the file I'm trimming and "save" is the location I'm saving. The trimming works but not fully. The new file does start from 60 seconds but it keeps going and not stopping at 90 seconds. For example if the file is 3 minutes it will start at 1 minute and end at 3. Its like the while is always true. What am I doing wrong here?

Thanks in advance!

EldarGoren
  • 107
  • 11

2 Answers2

0

I have no idea what your Mp3FileReader is doing there. But your while loop looks odd. Does mp3FileRead.ReadNextFrame() also change mp3FileReader.CurrentTime ? If not then there is your problem. You should atleast do mp3FileReader.CurrentTime + 1Frame. Otherwise your currenttime is never changed and loop will always be true

Nawed Nabi Zada
  • 2,819
  • 5
  • 29
  • 40
  • I think it should. I'ts from the library NAudio. I'm learning through Mark Heath course (the author of NAudio) and thats the code he wrote but instead of giving file location for open and save he made it defult. – EldarGoren Feb 03 '17 at 22:24
  • @EldarGoren Can you link to documentation ? – Nawed Nabi Zada Feb 03 '17 at 22:26
  • Do you mean NAudio documentation? if so :https://naudio.codeplex.com/documentation – EldarGoren Feb 03 '17 at 22:28
  • you said it was the code Mark wrote, is that where he is trimming a WAV file ? http://mark-dot-net.blogspot.nl/2009/09/trimming-wav-file-using-naudio.html – Nawed Nabi Zada Feb 03 '17 at 22:40
  • I'm learning through pluralsight. He made videos in there.https://app.pluralsight.com/library/courses/audio-programming-naudio/table-of-contents but its costs money so I don't think you will be able to see it. – EldarGoren Feb 03 '17 at 22:42
  • Have you check this answer: http://stackoverflow.com/a/14169073/2289942 – Nawed Nabi Zada Feb 03 '17 at 22:57
0

In NAudio 1.8.0, Mp3FileReader.ReadNextFrame does not progress CurrentTime, although I checked in a fix for that recently.

So you can either get the latest NAudio code, or make use of the SampleCount property on each Mp3Frame to accurately keep track of how far through you are yourself.

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • I'm already using NAudio 1.8.0 I'm pretty new to this so there is a way that I can download the fix? If not ill try mess with the sample count. – EldarGoren Feb 04 '17 at 18:33
  • I added position variable to the code like you did in the fix. `var position = 0; while (mp3FileReader.CurrentTime < endPosition) { var frame = mp3FileReader.ReadNextFrame(); if (frame == null) break; writer.Write(frame.RawData, 0, frame.RawData.Length); position += frame.SampleCount * mp3FileReader.WaveFormat.BitsPerSample; }` how do I add position to the `mp3FileReader.CurrentTIme` to fix the while? – EldarGoren Feb 04 '17 at 19:40