如何使用just_audio和audio_service同时播放两个(或更多)音频文件? [英] How to play two (or more) audio files simultaneously with just_audio and audio_service?

查看:119
本文介绍了如何使用just_audio和audio_service同时播放两个(或更多)音频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通读Github上的项目自述文件和问题,以查看是否有可能使用 just_audio audio_service 插件同时播放两个或多个音频文件扑.有没有人使用这些插件或类似的插件实现了类似的功能?我目前在生产中使用这些插件,因此最好坚持使用它们以向应用程序添加所需的功能.

I tried reading through the project readme and issues on Github to see if it was a possibility to play two or more audio files simultaneously using the just_audio and audio_service plugins for Flutter. Has anyone achieved something similar using these plugins or similar ones? I currently use these plugins in production so it'd be preferable to stick with them to add the desired functionality to the app.

任何帮助将不胜感激,谢谢!

Any help would be greatly appreciated, thanks!

推荐答案

在just_audio中,您可以创建 AudioPlayer 的两个实例,并将它们设置为播放不同的音频源.例如

In just_audio, you can create two instances of AudioPlayer and set them to play different audio sources. e.g.

final player1 = AudioPlayer();
final player2 = AudioPlayer();

await player1.setUrl(url1);
await player2.setUrl(url2);
...
player1.play(); // don't await
player2.play(); // don't await

如果您等待每个播放请求,则直到等待第一个播放完成后,它才开始播放第二个播放请求.因此,通过等待,第二个播放请求应在第一个播放请求之后的毫秒内触发,并且两个播放请求将并行播放.

If you await each play request, it would not start playing the second one until after waiting for the first one to finish playing. So by not awaiting, the second play request should be triggered within milliseconds after the first one, and both will play in parallel.

如果出于任何原因需要等到两个玩家都完成游戏后,则可以使用 Future.wait :

If for any reason you need to wait until both players have finished playing, you can use Future.wait:

await Future.wait([
  player1.play(),
  player2.play(),
]);
print('done'); // this will print after both players are finished.

(基于您的评论的其他示例)要循环播放 player2 直到 player1 完成:

(Extra example based on your comment) To loop player2 until player1 completes:

await player2.setLoopMode(LoopMode.one);
player2.play(); // don't await
await player1.play(); // wait for player1 to complete
await player1.pause(); // or stop or dispose
await player2.pause(); // or stop or dispose

请注意,每个玩家都用尽了本机资源,这意味着两件事:

Note that each player uses up native resources, which means two things:

  1. 如果创建太多并行实例,则可能会达到资源限制.
  2. 使用完每个播放器后,应在每个播放器上调用 dispose ,因为这将释放这些资源供其他应用程序使用.
  1. You may hit a resource limit if you create too many parallel instances.
  2. You should call dispose on each player once you're finished using it since this will free up those resources for use by other apps.

在audio_service方面,您基本上是在实现当用户按下播放按钮等时想要发生的事情的回调,并且您可以将上面的代码粘贴到您的回调中.例如,如果您想在用户单击播放"时同时播放两个播放器,则可以这样实现回调:

On the audio_service side, you are basically implementing callbacks for what you want to happen when the user presses the play button, etc, and you can stick the above code into your callback. For example, if you want to play both players simultaneously when the user clicks "play", you would implement your callback like this:

Future<void> onPlay() async {
  player1.play();
  player2.play();
}

您还可以类似地实现暂停回调:

You could also implement the pause callback similarly:

Future<void> onPause() async {
  player1.pause();
  player2.pause();
}

这篇关于如何使用just_audio和audio_service同时播放两个(或更多)音频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆