audioplayers icon indicating copy to clipboard operation
audioplayers copied to clipboard

How to get the total length of audio

Open CarminBack opened this issue 4 years ago • 9 comments

I need to get the length of the audio and adjust the progress of the audio

CarminBack avatar Feb 07 '21 01:02 CarminBack

    player.setUrl(url);
    if (Platform.isIOS) {
      length = await player.getDuration();
    } else {
      final duration = await player.onDurationChanged.first;
      length = duration.inMilliseconds;
    }

should work just fine.

mvarendorff avatar Feb 19 '21 11:02 mvarendorff

Is this reliable? On Android, I get incorrect duration for these files, see attached files. The incorrect duration is reported both in emulator and physical device. The first file (a.mp3) is 876 ms long according to Audacity (a separate program). This duration calculation from audioplayers reports it as 892 ms. That's 16ms diff (< 2%), I'm fine with that. But for the other file (b.mp3) Audacity reports 1 738 ms which seems correct, audioplayers duration reports 11 451 ms, more than 6x the correct duration. Why is that?

I can't upload mp3s here so I've pasted them into a filebin: https://filebin.net/ninfvdewwt0bt2j2

If anyone know how to get the mp3 audio duration in a reliable (and preferably quick) manner, please let me know.

mikeesouth avatar Feb 24 '21 21:02 mikeesouth

Yourt files are not available aymore, but I guess you're using VBR (Variable BitRate) files. Calculations are off on these files. Once you use either CBR encoded mp3 the time will be returned correctly.

mzohren avatar Mar 26 '21 12:03 mzohren

@mzohren I see, thanks! Here is the file (b.mp3) uploaded again: https://filebin.net/ninfvdewwt0bt2j2 Any tip on how I can determine if it's saved with VBR? I checked the duration using Audacity but it does not seem to show whether it's saved with VBR or CBR.

mikeesouth avatar Mar 26 '21 13:03 mikeesouth

I followed this superuser post and got this result:

$ mpck b.mp3
SUMMARY: b.mp3
    version                       MPEG v1.0
    layer                         3
    average bitrate               208747 bps (VBR)
    samplerate                    44100 Hz
    frames                        67
    time                          0:01.750
    unidentified                  0 b (0%)
    errors                        none
    result                        Ok

So it seems that indeed that file is using VBR which would confirm the suspicion mzohren had.

mvarendorff avatar Mar 26 '21 18:03 mvarendorff

I followed this superuser post and got this result:

$ mpck b.mp3
SUMMARY: b.mp3
    version                       MPEG v1.0
    layer                         3
    average bitrate               208747 bps (VBR)
    samplerate                    44100 Hz
    frames                        67
    time                          0:01.750
    unidentified                  0 b (0%)
    errors                        none
    result                        Ok

So it seems that indeed that file is using VBR which would confirm the suspicion mzohren had.

Nice, thank you! And thanks to @mzohren too.

mikeesouth avatar Mar 27 '21 10:03 mikeesouth

    player.setUrl(url);
    if (Platform.isIOS) {
      length = await player.getDuration();
    } else {
      final duration = await player.onDurationChanged.first;
      length = duration.inMilliseconds;
    }

should work just fine.

Im getting this error on IOS Fatal error: Double value cannot be converted to Int because it is either infinite or NaN: file Swift/x86_64-apple-ios-simulator.swiftinterface, line 32293

AhmadFadilB avatar May 10 '21 09:05 AhmadFadilB

Effectively it's a good workaround... May be you can integrate it directly in the library?

dan-gandolfo avatar Jul 19 '21 10:07 dan-gandolfo

    player.setUrl(url);
    if (Platform.isIOS) {
      length = await player.getDuration();
    } else {
      final duration = await player.onDurationChanged.first;
      length = duration.inMilliseconds;
    }

should work just fine.

Thanks a lot!! I wasted like 5 hours looking up for the problem. Totally works (just tested it on android). Thank you so much.

sherly26 avatar Nov 25 '21 17:11 sherly26

Mp3 isn't reliably in terms of duration. For mp3s with Variable Bit Rate, you have to calculate it while playing. This is dependent on the implementations of the media players on the underlying platforms.

See: https://stackoverflow.com/questions/10437750/how-to-get-the-real-actual-duration-of-an-mp3-file-vbr-or-cbr-server-side

Gustl22 avatar Sep 27 '22 13:09 Gustl22