我有一个按钮,当我点击它播放,点击声音。在我的第一次点击它不播放声音,第二次点击播放声音,如果我等待3-4秒,并单击按钮没有声音再次。但总是点击打印。
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')),
),
);
}
}我已经试过答案了。没有声音问题,但:)当页面init在我的第一次单击控制台输出是
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和其他点击输出,如
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发布于 2020-11-18 20:22:45
尝尝这个。
_audioCache = AudioCache(
prefix: "assets/sounds/",
fixedPlayer: AudioPlayer(
// set mode to LOW_LATENCY for better performance with short audio files
mode: PlayerMode.LOW_LATENCY,
)..setReleaseMode(ReleaseMode.STOP))
// pre-cache the audio file so it doesn't have to load on first call
..load('buttonClick.wav');发布于 2022-02-28 18:29:10
如果您在使用AudioPlayers颤振包时遇到错误。
转到pubspec.yaml文件并加载资产中的所有声音。例如
flutter:
assets:
- assets/note1.wav
- assets/youngpro.mp3https://stackoverflow.com/questions/64880967
复制相似问题