Flutter: Fatal Error: Callback lookup failed! (with audioplayers package)
Flutter: Fatal Error: Callback lookup failed! (with audioplayers package)
以下代码片段是点击按钮时会发出一声短促的哔声:
FlatButton(
onPressed: () => {
final player = new AudioCache();
player.play('note1.wav');
}
child: Text('Click to ding!'),
),
我面临的问题是每当我单击此按钮时,虽然声音播放正确,但我在终端中收到此错误:flutter: Fatal Error: Callback lookup failed!
我想知道它是什么以及我应该做什么?
我检查了 package's issue page 但没有发现任何有用的信息。
谢谢
我遇到了同样的问题,所以这适用于我的游戏代码,我希望这对你有用,首先,像这样创建一个控制器 Class:
import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';
void audioPlayerHandler(AudioPlayerState value) => print('state => $value');
class GameController {
static AudioPlayer audioPlayer = AudioPlayer();
static AudioCache audioCache = AudioCache();
static void play(String sound) {
if (!kIsWeb && Platform.isIOS) {
audioPlayer.monitorNotificationStateChanges(audioPlayerHandler);
}
audioCache.play(sound);
}
}
然后通过这种方式使用您的代码:
FlatButton(
onPressed: () => {
GameController.play('note1.wav');
}
child: Text('Click to ding!'),
),
看起来像是“致命错误:回调查找失败!”的错误。如果未通过 monitorNotificationStateChanges
.
定义回调方法,则会打印在日志中
By reading the source code好像没有发生过
但避免它的一种方法确实是设置回调,并且必须在 外部 您的 class 定义它,如下所示:
import 'package:audioplayers/audioplayers.dart';
class YourStatefulWidget extends StatefulWidget {
@override
_YourStatefulWidgetState createState() => _YourStatefulWidgetState();
}
class _YourStatefulWidgetState extends State<YourStatefulWidget> {
...
@override
void initState() {
super.initState();
if (Platform.isIOS) {
// to avoid getting "Fatal Error: Callback lookup failed!"
audioPlayer.monitorNotificationStateChanges(audioPlayerHandler);
}
}
...
}
// must be defined globally
void audioPlayerHandler(AudioPlayerState value) => null;
以下代码片段是点击按钮时会发出一声短促的哔声:
FlatButton(
onPressed: () => {
final player = new AudioCache();
player.play('note1.wav');
}
child: Text('Click to ding!'),
),
我面临的问题是每当我单击此按钮时,虽然声音播放正确,但我在终端中收到此错误:flutter: Fatal Error: Callback lookup failed!
我想知道它是什么以及我应该做什么?
我检查了 package's issue page 但没有发现任何有用的信息。
谢谢
我遇到了同样的问题,所以这适用于我的游戏代码,我希望这对你有用,首先,像这样创建一个控制器 Class:
import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';
void audioPlayerHandler(AudioPlayerState value) => print('state => $value');
class GameController {
static AudioPlayer audioPlayer = AudioPlayer();
static AudioCache audioCache = AudioCache();
static void play(String sound) {
if (!kIsWeb && Platform.isIOS) {
audioPlayer.monitorNotificationStateChanges(audioPlayerHandler);
}
audioCache.play(sound);
}
}
然后通过这种方式使用您的代码:
FlatButton(
onPressed: () => {
GameController.play('note1.wav');
}
child: Text('Click to ding!'),
),
看起来像是“致命错误:回调查找失败!”的错误。如果未通过 monitorNotificationStateChanges
.
By reading the source code好像没有发生过
但避免它的一种方法确实是设置回调,并且必须在 外部 您的 class 定义它,如下所示:
import 'package:audioplayers/audioplayers.dart';
class YourStatefulWidget extends StatefulWidget {
@override
_YourStatefulWidgetState createState() => _YourStatefulWidgetState();
}
class _YourStatefulWidgetState extends State<YourStatefulWidget> {
...
@override
void initState() {
super.initState();
if (Platform.isIOS) {
// to avoid getting "Fatal Error: Callback lookup failed!"
audioPlayer.monitorNotificationStateChanges(audioPlayerHandler);
}
}
...
}
// must be defined globally
void audioPlayerHandler(AudioPlayerState value) => null;