Farrel Anelca
Farrel Anelca

Reputation: 137

Flutter Sound (Looping)

i already made voice recorder using flutter_sound,

the sound only play once, maybe someone have try to auto loop when it's play ?

@override
  void initState()  {
    if (widget.voiceofer != null) {
      flutterSound.startPlayer(
          '/data/user/0/id.captrue.captrue/app_flutter/${widget.voiceofer}'); 
    }
    super.initState();
  }

Upvotes: 8

Views: 10309

Answers (3)

Mostafa Mahmoud
Mostafa Mahmoud

Reputation: 524

"AudioPlayer" works well for me with this implementation:

//Making sound looping.

final bgAudio = AudioPlayer();

void playBackgroundMusic() {
  bgAudio.play(
    AssetSource('audio/background.mp3'),
  );
  bgAudio.onPlayerComplete.listen((event) {
    bgAudio.play(
      AssetSource('audio/background.mp3'),
    );
  });

Upvotes: 2

Sanay Varghese
Sanay Varghese

Reputation: 119

Update

using new version of audioplayers 1.1.1 you can use setReleaseMode()

Example:

final player = AudioPlayer();

void playSound() async {
    await player.play(AssetSource('sound.mp3'));   
}
void loop() {
    player.setReleaseMode(ReleaseMode.loop);
}

Upvotes: 9

Mariano Zorrilla
Mariano Zorrilla

Reputation: 7686

I recommend to use audioplayers as library who has a build in feature for looping audio.

Link: audioplayers: ^0.14.0

Implementation:

  • Create an AudioCache instance with the path for your audios (example: "/audio") inside your Assets folder.
  • Use the Future call loop the with name of your file.
  • That will create an instance of AudioPlayer to handle Pause and Stop.

Example:

static AudioCache musicCache;
static AudioPlayer instance;

void playLoopedMusic() async {
    musicCache = AudioCache(prefix: "audio/");
    instance = await musicCache.loop("bgmusic.mp3");
    // await instance.setVolume(0.5); you can even set the volume
  }

void pauseMusic() {
  if (instance != null) {
    instance.pause();
  }
}

Upvotes: 2

Related Questions