如何合并两个音频文件与just_audio播放在同一时间?



如何合并(不是concat)两个音频文件与just_audio同时播放?

我想合并它,以避免在同一时间播放just_audio实例的硬件限制。只使用just_audio.

我发现连接多个音频文件的唯一方法是使用ffmpeg。将此添加到您的pub.devflutter_ffmpeg并将该类添加到您的lib文件夹:

class FFmpeg {
static Future<File> concatenate(List<String> assetPaths, {String output = "new.mp3"})async{
final directory = await getTemporaryDirectory();
final file = File("${directory.path}/$output");
final ffm = FlutterFFmpeg();
final cmd = ["-y"];
for(var path in assetPaths){
final tmp = await copyToTemp(path);
cmd.add("-i");
cmd.add(tmp.path);
}
cmd.addAll([
"-filter_complex",
"[0:a] [1:a] concat=n=${assetPaths.length}:v=0:a=1 [a]",
"-map", "[a]", "-c:a", "mp3", file.path
]);
await ffm.executeWithArguments(cmd);
return file;
}
static Future<File>copyToTemp(String path)async{
Directory tempDir = await getTemporaryDirectory();
final tempFile = File('${tempDir.path}/${path.split("/").last}');
if(await tempFile.exists()){
return tempFile;
}
final bd = await rootBundle.load(path);
await tempFile.writeAsBytes(bd.buffer.asUint8List(), flush: true);
return tempFile;
}
}

的例子:

final track = await FFmpeg.concatenate(
[
"assets/audios/test1.mp3",
"assets/audios/test2.mp3",
"assets/audios/test3.mp3",
],
output: "output.mp3"
);

相关内容

  • 没有找到相关文章

最新更新