Quantcast
Channel: NAudio
Viewing all articles
Browse latest Browse all 5831

New Post: Mp3FileReader not returning CurrentTime and Position

$
0
0
As a work around, I altered the program to calculate it's own CurrentTime rather than relying on reader.CurrentTime as follows.
I'm back in business ...

public static void TrimMp3File(string inputPath, string outputPath, TimeSpan? begin, TimeSpan? end)
        {
            //  This routine trims an MP3 file
            //     Ref: http://stackoverflow.com/questions/7932951/trimming-mp3-files-using-naudio
            //            * original code from Mark Heath (Naudio creator)
            //          http://stackoverflow.com/questions/6220660/calculating-the-length-of-mp3-frames-in-milliseconds
            //            * frame length in ms formula: lucianolev

            double currentSecond = 0.0D; //                                         2016-11-26: Start - fix: replaced reliance on reader.CurrentTime (which is not being updated)
            double beginSecond = 0.0D;   //                                                                  with the calculated currentSecond.
            double endSecond = 9999.0D;                                                             
            TimeSpan totalDuration = new TimeSpan(0, 0, 0); //                      2016-11-26: End
           
            if (begin.HasValue && end.HasValue && begin > end)
                throw new ArgumentOutOfRangeException("end", "end should be greater than begin");

            using (var reader = new Mp3FileReader(inputPath)) {
                totalDuration = reader.TotalTime; //                                2016-11-26: Start
                if (begin.HasValue) beginSecond = ((TimeSpan)begin).TotalSeconds;
                if (end.HasValue) endSecond = ((TimeSpan)end).TotalSeconds;
                else endSecond = totalDuration.TotalSeconds; //                     2016-11-26: End
                
                using (var writer = File.Create(outputPath)) {
                    Mp3Frame frame;
                    while ((frame = reader.ReadNextFrame()) != null) {
                         currentSecond += ((double)frame.SampleCount / (double)frame.SampleRate); //    2016-11-26
                         if (currentSecond >= beginSecond) { //                                         2016-11-26                                                    
                            if (currentSecond <= endSecond)  //                                         2016-11-26
                                writer.Write(frame.RawData, 0, frame.RawData.Length);
                            else break;
                        }
                    }
                }
            }
        }

Viewing all articles
Browse latest Browse all 5831

Latest Images

Trending Articles



Latest Images