audioplayers
audioplayers copied to clipboard
How to get the total length of audio
I need to get the length of the audio and adjust the progress of the audio
player.setUrl(url);
if (Platform.isIOS) {
length = await player.getDuration();
} else {
final duration = await player.onDurationChanged.first;
length = duration.inMilliseconds;
}
should work just fine.
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.
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 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.
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.
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.
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
Effectively it's a good workaround... May be you can integrate it directly in the library?
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.
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