How can I fetch the metadata of audio files in flutter?

7k views Asked by At

Metadata of an audio file is most important things in audio related apps. With the help of meta data we can use title, author name, artist name, album name, cover image and other details so we can use those details in our app to list the music items and able to create different groups or partition accordingly. For example if we create the music app so if the 6 songs is from imagine dragon and other 6 song from one republic and also some if some songs of imagine dragon but from different album so we can categories them based on certain criteria of metadata.

In assets_audio_player we can add metadata manually but could not fetch existing metadata from the files. And as far as i know through exploring the other audio packages in pub.dev most of other packages does not support that.

2

There are 2 answers

1
FloatingRock On

Found this package that may help: https://pub.dev/packages/id3/ (assuming you're using MP3 files; you don't mention the format)

EDIT: Here's another https://pub.dev/packages/dart_tags

3
Hitesh On

Here is a package for Flutter to read metadata of all kind of audio files & even from URLs.

https://github.com/alexmercerind/flutter_media_metadata

You can use it as follows:

Example

import 'package:flutter_media_metadata/flutter_media_metadata.dart';

var retriever = new MetadataRetriever();
await retriever.setFile(new File('/storage/emulated/0/Music/music.aac'));
Metadata metadata = await retriever.metadata;

metadata.trackName;
metadata.trackArtistNames;
metadata.albumName;
metadata.albumArtistName;
metadata.trackNumber;
metadata.albumLength;
metadata.year;
metadata.genre;
metadata.authorName;
metadata.writerName;
metadata.discNumber;
metadata.mimeType;
metadata.trackDuration;
metadata.bitrate;

retriever.albumArt;

await retriever.setUri(new Uri.https('www.example.com', '/audio.MP3', {}));

Map<String, dynamic> map = await metadata.toMap();

Demo

The packages mentioned by @FloatingRock are written in Dart itself, thus slow. And support only MP3 tags (i.e. only ID3v1/ID3v2).