Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
  }
like image 757
Farrel Anelca Avatar asked Jun 17 '26 03:06

Farrel Anelca


2 Answers

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);
}
like image 153
Sanay Varghese Avatar answered Jun 18 '26 19:06

Sanay Varghese


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();
  }
}
like image 43
Mariano Zorrilla Avatar answered Jun 18 '26 20:06

Mariano Zorrilla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!