Flutter音频标签编辑插件audiotagger的使用

Flutter音频标签编辑插件audiotagger的使用

audiotagger

构建状态 发布版本

此库允许你读取和写入MP3文件的ID3标签。
基于JAudiotagger库。

注意:该库目前仅在Android上有效。

添加依赖

dependencies:
  audiotagger: ^2.2.1

Audiotagger需要访问读取和写入存储权限。
你可以使用权限处理库来实现这一点。

目录

基本用法

初始化一个新的标记器实例;

final tagger = new Audiotagger();

读取操作

读取标签作为Tag对象

获取文件的ID3标签作为Tag对象。

void getTags() async {
    final String filePath = "/storage/emulated/0/file.mp3";
    final Tag tag = await tagger.readTags(
        path: filePath
    );
}

注意:这种方法不会读取歌曲的封面艺术。为此,请使用readArtwork方法。

Tag对象具有以下模式:Tag对象模式

读取标签作为Map

获取文件的ID3标签作为Map

void getTagsAsMap() async {
    final String filePath = "/storage/emulated/0/file.mp3";
    final Map map = await tagger.readTagsAsMap(
        path: filePath
    );
}

注意:这种方法不会读取歌曲的封面艺术。为此,请使用readArtwork方法。

Map具有以下模式:Map of Tag模式

读取封面艺术

获取歌曲的封面艺术作为Uint8List

void getArtwork() async {
    final String filePath = "/storage/emulated/0/file.mp3";
    final Uint8List bytes = await tagger.readArtwork(
        path: filePath
    );
}

读取音频文件作为AudioFile对象

获取MP3文件的信息作为AudioFile对象。

void getAudioFile() async {
    final String filePath = "/storage/emulated/0/file.mp3";
    final AudioFile audioFile = await tagger.readAudioFile(
        path: filePath
    );
}

AudioFile对象具有以下模式:AudioFile对象模式

读取音频文件作为Map

获取MP3文件的信息作为Map

void getAudioFileAsMap() async {
    final String filePath = "/storage/emulated/0/file.mp3";
    final Map map = await tagger.readAudioFileAsMap(
        path: filePath
    );
}

Map具有以下模式:Map of AudioFile模式

写入操作

从Map写入标签

你可以从一个Map写入ID3标签。
要重置一个字段,请传递一个空字符串("")。
如果值为null,则该字段将被忽略且不会写入。

void setTagsFromMap() async {
    final path = "storage/emulated/0/Music/test.mp3";
    final tags = <String, String>{
        "title": "Title of the song",
        "artist": "A fake artist",
        "album": "",    //This field will be reset
        "genre": null,  //This field will not be written
    };

    final result = await tagger.writeTagsFromMap(
        path: path,
        tags: tags
    );
}

Map具有以下模式:Map of Tag模式

从Tag对象写入标签

你可以从一个Tag对象写入ID3标签。
要重置一个字段,请传递一个空字符串("")。
如果值为null,则该字段将被忽略且不会写入。

void setTags() async {
    final path = "storage/emulated/0/Music/test.mp3";
    final tag = Tag(
        title: "Title of the song",
        artist: "A fake artist",
        album: "",    //This field will be reset
        genre: null,  //This field will not be written
    );

    final result = await tagger.writeTags(
        path: path,
        tag: tag,
    );
}

Tag对象具有以下模式:Tag对象模式

写入单个标签字段

你可以通过指定字段名写入单个标签字段。
要重置该字段,请传递一个空字符串("")。
如果值为null,则该字段将被忽略且不会写入。

void setTags() async {
    final path = "storage/emulated/0/Music/test.mp3";

    final result = await tagger.writeTag(
        path: path,
        tagField: "title",
        value: "Title of the song"
    );
}

有关字段名称,请参阅Map of Tag模式

模型

这些是Audiotagger请求和返回的Map和类的模式。

Tag类

String? title;
String? artist;
String? genre;
String? trackNumber;
String? trackTotal;
String? discNumber;
String? discTotal;
String? lyrics;
String? comment;
String? album;
String? albumArtist;
String? year;
String? artwork; // 表示歌曲封面的艺术路径。

Map of Tag

<String, String>{
    "title": value,
    "artist": value,
    "genre": value,
    "trackNumber": value,
    "trackTotal": value,
    "discNumber": value,
    "discTotal": value,
    "lyrics": value,
    "comment": value,
    "album": value,
    "albumArtist": value,
    "year": value,
    "artwork": value, // 如果是从readTags或readTagsAsMap获取,则为null;
};

AudioFile类

int? length;
int? bitRate;
String? channels;
String? encodingType;
String? format;
int? sampleRate;
bool? isVariableBitRate;

Map of AudioFile

<String, dynamic?>{
    "length": length,
    "bitRate": bitRate,
    "channels": channels,
    "encodingType": encodingType,
    "format": format,
    "sampleRate": sampleRate,
    "isVariableBitRate": isVariableBitRate,
};

更多关于Flutter音频标签编辑插件audiotagger的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter音频标签编辑插件audiotagger的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用audiotagger插件来编辑音频标签的示例代码。这个插件允许你读取和写入MP3文件的ID3标签。

首先,确保你已经将audiotagger插件添加到你的pubspec.yaml文件中:

dependencies:
  flutter:
    sdk: flutter
  audiotagger: ^0.4.0  # 请注意版本号,根据实际情况选择最新版本

然后运行flutter pub get来安装依赖。

接下来,你可以在你的Flutter项目中使用以下代码来编辑音频文件的标签。

示例代码

  1. 导入必要的包
import 'package:flutter/material.dart';
import 'package:audiotagger/audiotagger.dart';
import 'dart:io';
  1. 选择音频文件并编辑标签
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Audio Tagger Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              // 选择音频文件
              FilePickerResult? result = await FilePicker.platform.pickFiles(
                type: FileType.audio,
                allowMultiple: false,
              );

              if (result != null && result.files.isNotEmpty) {
                File audioFile = File(result.files.first.path!);

                // 读取当前标签
                AudioFile audio = await AudioFile.fromPath(audioFile.path);
                Map<String, String> tags = await audio.readAllTags();

                print('Current Tags: $tags');

                // 编辑标签
                Map<String, String> newTags = {
                  'title': 'New Title',
                  'artist': 'New Artist',
                  'album': 'New Album',
                  'year': '2023',
                  'comment': 'This is a new comment',
                  'genre': 'Pop',
                };

                // 写入新标签
                await audio.writeTags(newTags);

                print('Tags Updated Successfully');

                // 再次读取标签以验证更改
                Map<String, String> updatedTags = await audio.readAllTags();
                print('Updated Tags: $updatedTags');
              }
            },
            child: Text('Select and Edit Audio File'),
          ),
        ),
      ),
    );
  }
}

注意事项

  1. 文件选择器:上面的代码使用了file_picker插件来选择音频文件。你需要将此插件也添加到你的pubspec.yaml文件中:
dependencies:
  file_picker: ^4.3.4  # 请注意版本号,根据实际情况选择最新版本
  1. 权限:在Android上,你可能需要在AndroidManifest.xml中请求存储权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

并在运行时请求这些权限(在Flutter中,你可以使用permission_handler插件来处理权限请求)。

  1. 错误处理:在实际应用中,你应该添加错误处理逻辑来处理文件选择失败、文件读写失败等情况。

  2. 插件版本:请确保你使用的插件版本是最新的,或者至少是一个稳定的版本,以避免潜在的bug。

通过上述代码,你可以在Flutter应用中轻松地选择和编辑音频文件的ID3标签。

回到顶部