swing13
swing13

Reputation: 53

Playing the same audio file multiple times - Audioplayers Flutter

How can I play the same audio file as many times as I want, not loop but I want the possibility to precise how many times, I followed this tutorial to make the basics which mean the play/pause function. And I'm struggling with the rest. I use the last version of the audioplayers package.

This is my code :

import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:audioplayers/audio_cache.dart';


class Player extends StatefulWidget {
  @override
  _PlayerState createState() => _PlayerState();
}

AnimationController _animationIconController ;

AudioCache audioCache;
AudioPlayer audioPlayer;


bool issongplaying = false;
bool isplaying = false;



class _PlayerState extends State<Player> with TickerProviderStateMixin {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initPlayer();
  }

  void initPlayer(){
    _animationIconController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 750),
      reverseDuration: Duration(milliseconds: 750),
    );
    audioPlayer = new AudioPlayer();
    audioCache = new AudioCache(fixedPlayer: audioPlayer);
   
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            GestureDetector(
              onTap: (){
                setState((){
                  isplaying
                  ? _animationIconController.reverse()
                  : _animationIconController.forward();
                  isplaying = !isplaying;
                });

                if (issongplaying == false) {
                  audioCache.play('audio.wav');
                  setState(() {
                    issongplaying = true;
                  });
                } else {
                  audioPlayer.pause();
                  setState(() {
                    issongplaying = false;
                  });
                }
              },
              child: ClipOval(
                child: Container(
                  decoration: BoxDecoration(
                    border: Border.all(
                    width: 2,
                      color: Colors.greenAccent,
                    ),
                    borderRadius: BorderRadius.all(Radius.circular(50.0)
                    ),
                  ),
                  width: 75,
                  height: 75,
                  child: Center(
                    child: AnimatedIcon(
                      icon: AnimatedIcons.play_pause,
                      progress: _animationIconController,
                      color: Colors.grey,
                      size: 60,
                    )
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

Thank you

Upvotes: 1

Views: 3890

Answers (1)

janavarro
janavarro

Reputation: 889

This might be complex for you but try giving it a shot.

I can see the audioplayers package has the event onPlayerCompletion, which will be called each time you finish playing an audio.

You can tell your player in this event to track the amount of times it ends and set the audio to loop, it will stop when it repeats X times:

int timesPlayed = 0;
int timesToRepeat = 3; //The audio will repeat 3 times

//This method gets called each time your audio finishes playing.
player.onPlayerCompletion.listen((event) {
  //Here goes the code that will be called when the audio finishes
  onComplete();
  setState(() {
    position = duration;
    timesPlayed++;
    if(timesPlayed >= timesToRepeat) {
      timesPlayed = 0;
      await player.stop();
    }
  });
});

Upvotes: 1

Related Questions