'audioplayers My button doesn't play sound in first click
I have a button and when I click it plays click sound. In my first click it doesn't play the sound, second click plays the sound and if I wait 3-4 sec and click the button no sound again. But always print CLICKED.
import 'package:flutter/material.dart';
import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';
class MyButton extends StatefulWidget {
@override
_MyButtonState createState() => _MyButtonState();
}
class _MyButtonState extends State<MyButton> {
AudioCache _audioCache;
@override
void initState() {
super.initState();
_audioCache = AudioCache(
prefix: "assets/sounds/",
fixedPlayer: AudioPlayer()..setReleaseMode(ReleaseMode.STOP));
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
print('CLICKED');
_audioCache.play('buttonClick.wav');
},
child: Container(
height: 80,
width: 80,
decoration: BoxDecoration(
color: Colors.green,
shape: BoxShape.circle,
),
child: Center(child: Text('CLICK ME')),
),
);
}
}
Ive tried the answer code. There is no sound problem but :) when page init in my first click console output is
D/WSP (16110): Reusing soundId1 for /data/user/0/com.example.myapp/cache/buttonClick.wav is loading=false xyz.luan.audioplayers.WrappedSoundPool@bfc7a44
V/AudioTrack(16110): start: server read:-8418 cumulative flushed:0 client written:0
and other click outputs like this
I/flutter (16110): CLICKED
V/AudioTrack(16110): start: server read:-12627 cumulative flushed:0 client written:0
I/flutter (16110): CLICKED
V/AudioTrack(16110): start: server read:-14030 cumulative flushed:0 client written:0
I/flutter (16110): CLICKED
V/AudioTrack(16110): start: server read:-15433 cumulative flushed:0 client written:0
I/flutter (16110): CLICKED
V/AudioTrack(16110): start: server read:-16836 cumulative flushed:0 client written:0
Solution 1:[1]
If you're getting errors when working with AudioPlayers flutter package.
Go to pubspec.yaml file and load all the sounds in your assests. For example
flutter:
assets:
- assets/note1.wav
- assets/youngpro.mp3
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Apealed |
