Flutter audioplayers delay of sound when triggerd by button press

46 Views Asked by At

I am trying to make a music sampler, but the reaction of the sound when you press the button is to slow. How can I make sure that when you tap the button the sound starts to play as fast as possible?

This is how I do it now:

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

...

class ColorButton extends StatelessWidget {
  final Color color;

  const ColorButton({Key? key, required this.color}) : super(key: key);

  String getColorName(Color color) {
    if (color == Colors.red) return 'Red';
    if (color == Colors.blue) return 'Blue';
    return 'Unknown';
  }

  @override
  Widget build(BuildContext context) {
    return LongPressDraggable(
      feedback: Material(
        child: SizedBox(
          width: 50,
          height: 50,
          child: Container(
            color: color,
            child: const Icon(Icons.palette, color: Colors.black),
          ),
        ),
      ),
      childWhenDragging: Container(),
      child: GestureDetector(
        onTap: () async {
          print(getColorName(color)); // Print the color name
          // play the sound audio/my_audio.wav
          final player = AudioPlayer();
          await player.play(AssetSource('my_audio.wav'));
        },
        onLongPress: () {
          ...
            },
          );
        },
        child: Container(
          color: color,
          child: Center(
            child: Text(
              getColorName(color), // Display the color name
              style: const TextStyle(
                  color: Colors.black), // Set text color to black
            ),
          ),
        ),
      ),
    );
  }
}

I tried to initialize the player earlier, but results got worse.

0

There are 0 best solutions below