简体   繁体   English

Flutter - 如何在不缓冲的情况下播放两个相对较长的音频文件

[英]Flutter - How to play two relatively long audio files without buffering

I have two audio files.我有两个音频文件。 One is a robotic voice (2:11 long) and a music file (47 seconds long).一个是机器人声音(2:11 长)和一个音乐文件(47 秒长)。 The music is supposed to loop several times until the robotic voice is finished.音乐应该循环几次,直到机器人声音完成。 I've already mastered that (and therefore left it out of the code snippet), but I find that the voice keeps buffering.我已经掌握了这一点(因此将其排除在代码片段之外),但我发现声音一直在缓冲。 After some tests, I see this pattern.经过一些测试,我看到了这种模式。

  1. If the voice plays alone, it plays smoothly如果声音单独播放,播放流畅
  2. If the music plays alone, it plays smoothly如果音乐单独播放,则播放流畅
  3. If the voice & music (lowered volume) play together, only the voice starts buffering.如果语音和音乐(降低音量)一起播放,则只有语音开始缓冲。

I've tried switching from the just_audio package to the audioplayers package so that I can play the music on low_latency mode, but then the music doesn't play at all.我尝试从just_audio package 切换到audioplayers package 以便我可以在low_latency模式下播放音乐,但是音乐根本不播放。 I believe it's currently a bug.我相信它目前是一个错误。

Here's a simple test widget:这是一个简单的测试小部件:

class _TestWidgetState extends State<TestWidget> {
  AudioPlayer voice = AudioPlayer();
  AudioPlayer music = AudioPlayer();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(kPadding),
      child: ElevatedButton(
        onPressed: () {
          voice.play("https://cdn.prayershub.com/audio/6/124/322ff9ba35744b58a8ea73c7.mp3");
          music.play(
              "https://cdn.prayershub.com/hymns/french/hymns-et-lounages/vie-chretienne-consecration-et-sanctification/piano/a-jesus-je-mabandonne-325.mp3");
          music.setVolume(0.2);
          music.setReleaseMode(ReleaseMode.LOOP);
        },
        child: const Text("play"),
      ),
    );
  }

  @override
  void dispose() {
    voice.dispose();
    music.dispose();
    super.dispose();
  }
}

You can try using the low latency like this您可以尝试使用这样的低延迟

AudioPlayer voice = AudioPlayer(mode: PlayerMode.LOW_LATENCY);

Edit编辑

static final playerCache = AudioCache();
  static AudioPlayer audioPlayer;

  
    audioPlayer = await playerCache.play('$audioName.mp3');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM