Search code examples
dartaudioduration

How to get audio duration in dart (not flutter)?


I need to get audio duration (from file and url) in the dart package (NOT IN FLUTTER APP).

When I am trying to use packages from the pub.dev (like just_audio or similar) I receive an error like next:

/.flutter/packages/flutter/lib/src/services/binary_messenger.dart:6:8: Error: Not found: 'dart:ui'

So do you know the way how to get the duration in pure dart?


Solution

  • If you want to work only with mp3 files, you are lucky and you can use this package: mp3_info: ^0.2.0.

    Code example:

      //url
      var url = Uri.parse('https://some_path/file.mp3');
      var response = await http.get(url);
    
      final mp3 = MP3Processor.fromBytes(response.bodyBytes);
    
      //file
      // final mp3 = MP3Processor.fromFile(File('path_to_file'));
    
      print(mp3.bitrate);
      print(mp3.duration);
    
    

    But if you need to work with other formats, you have to parse the file's byte data according to audio formats specs... or you can try to find some c/c++ libraries and use them through ffi.