skip to Main Content

In case the user did not wait to hear
the sound of the letter
He moved between the buttons quickly
There is an overlap between the voices,
and all the voices remain .
in the background of the application
How to correct or solve the problem?

full code

https://github.com/Saleque474/Learn-Alphabet

enter image description here

dependencies : kplayer: ^0.2.4


class _HomeState extends State<Home> {
  String currentAlphabet = "A";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.amber[200],
      body: LayoutBuilder(builder: (context, constraints) {
        var height = constraints.maxHeight;
        var width = constraints.maxWidth;
        return Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            sentenceHolder(height, width),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                imageHolder(height, width),
                wordHolder(height, width),
              ],
            ),
            Container(
              decoration:
                  BoxDecoration(borderRadius: BorderRadius.circular(15)),
              child: Wrap(
                children: changePos(),
              ),
            ),
          ],
        );
      }),
    );
  }

  playAudio(String path) {
    Player.asset(path).play();
  }

  List<Widget> changePos() {
    List<Widget> children = [
      ...alphabets
          .map(
            (e) => textHolder(e),
          )
          .toList()
    ];

    return children;
  }

Widget textHolder(String alphabet) {
    return Padding(
      padding: const EdgeInsets.all(7),
      child: ElevatedButton(
        onPressed: () {
          currentAlphabet = alphabet;
          playAudio("assets/audio/$alphabet.wav");
          setState(() {});
        },
        child: Text(alphabet),
      ),
    );
  }

//
When calling a letter sound is a function

It works normally, but if we move quickly between the letter buttons, we notice that all the sounds work together at the same time
//

2

Answers


  1. One Simple solution is:
    stop the already playing song/sound before calling play()

    Login or Signup to reply.
  2. you can handle this case by following 3 steps:

    1- Create a public object from the player class(nullable)
    Make sure to declare it outside the method playAudio;
    Player? publicPlayer;

    2- before playing the sound make sure to stop the old sound

    if(publicPlayer != null) publicPlayer.stop();

    3- instead of calling Player.asset(path).play();
    call after assigning the publicPlayer;

    publicPlayer = Player.asset(path);
    publicPlayer.play();

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search