Flutter 音频播放器我的按钮没有';不要在第一次单击时播放声音
我有一个按钮,当我点击它播放点击声音。在我的第一次单击中,它不会播放声音,第二次单击会播放声音,如果我等待3-4秒,然后再次单击按钮“无声音”。但总是点击打印Flutter 音频播放器我的按钮没有';不要在第一次单击时播放声音,flutter,dart,Flutter,Dart,我有一个按钮,当我点击它播放点击声音。在我的第一次单击中,它不会播放声音,第二次单击会播放声音,如果我等待3-4秒,然后再次单击按钮“无声音”。但总是点击打印 import 'package:flutter/material.dart'; import 'package:audioplayers/audio_cache.dart'; import 'package:audioplayers/audioplayers.dart'; class MyButton extends StatefulW
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')),
),
);
}
}
和其他类似的点击输出
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
试试这个
_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');
你在测试什么?你的wav文件有多大?我对你的密码没有问题。在我自己的手机上。5kb。我也看不出有什么问题。我想这可能是一个电话相关的情况。这是可能的。请查看我的答案,以找到一种提高性能的方法,从而解决您的问题。谢谢,它可以工作,但我想我会遇到错误或其他问题:)我更新了我的问题。他们有问题吗?
_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');