Search code examples
flutterdartdelayaudio-player

How do I delayed audioplayer in flutter?


I want to connect two other mp3 files, but it just plays one mp3 file because the audio player does not play the mp3 file sequentially. And finally, I try to delay 5 seconds after below other mp3 file played like this.

  void getAudio() async{
    var url = _API_AUDIO_PREFIX;
    var path;
    int duration = 0;
    Map<String, dynamic> json = await server.postReq(data);
    print(json["voices"]);
    for(int i=0;i<json["voices"].length;i++){
      path = url + json["voices"][i]["title"];
      print(path);
      duration = i * 500;
      Future.delayed(const Duration(milliseconds: duration), (){
        playAudio(path);
      });
    }
  }
  Future<void> playAudio(var url) async {
    int result = await audioPlayer.play(url);
    if (result == 1){
      print("success");
    }
    //duration = await audioPlayer.getDuration();
  }

but in milliseconds, they have errors like this.
Evaluation of this constant expression throws an exception.
How do I fix this? And How do I played the mp3 file continue to other mp3 files?


Solution

  • I fixed error
    Evaluation of this constant expression throws an exception.

    to remove const in const Duration(milliseconds: duration).
    but it can't work continue to next files because path variable is changed equally. :(