Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Fatal Error: Callback lookup failed! (with audioplayers package)

The following snippet is the button by clicking which, a short beep plays:

FlatButton(
  onPressed: () => {
    final player = new AudioCache();
    player.play('note1.wav');
  }
  child: Text('Click to ding!'),
),

The problem I'm facing is whenever I click this button, although sound plays correctly, I get this error in terminal: flutter: Fatal Error: Callback lookup failed! I wanna know what it is and what I should do?

I checked the package's issue page but didn't find anything helpful.

Thanks

like image 610
Amir Noureddini Avatar asked Apr 17 '20 03:04

Amir Noureddini


2 Answers

I faced the same issue, so this works for my game code, I hope this works for you, first, create a Controller Class like this:

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);
  }
}

Then use your code by this way:

FlatButton(
  onPressed: () => {
    GameController.play('note1.wav');
  }
  child: Text('Click to ding!'),
),
like image 177
rafaelwms Avatar answered Oct 06 '22 09:10

rafaelwms


Looks like a bug where "Fatal Error: Callback lookup failed!" is printed in the logs if no callback method was defined via monitorNotificationStateChanges.

By reading the source code it seems like this doesn't have any incidence.

But a way to avoid it is indeed to set that callback, and it has to be defined outside your class like so:

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;
like image 24
pierrea Avatar answered Oct 06 '22 11:10

pierrea