Can we detect when no speaker is plugged in



  • Hi all, when loading sounds on Windows I get "AL lib: (EE) MMDevApiOpenPlayback: Device init failed: 0x80070490" when there is no audio device plugged in.

    In this case, sound objects loaded with FlxG.sound.cache() + FlxG.sound.load() appear valid, but will crash the application when calling play() on them (more precisely: it's an "Invalid operation (/)" "Called from flixel.system.FlxSound::update line 258")

    Is there a way to determine when the underlying sound system is not working properly? I would like to disable sounds in this case, rather than crashing.

    Edit: haxe 3.3.0, haxeflixel 4.1.1, lime: 2.9.1, openfl: 3.6.1

    Thanks!



  • Ok, I think the following correctly catches errors related to the sound system:

    @:access(flixel.system.frontEnds.SoundFrontEnd.update)
    static public function play(soundId:String) {
      if (!enabled) return;
      var sound:FlxSound = FlxG.sound.load(FlxG.sound.cache(soundId), 1, false, false);
      try {
        sound.play();
        FlxG.sound.update(0); /* This will fail and be caught if there is an issue with sounds (like no audio device present) */
      } catch (e:Dynamic) {
        trace('Warning: Failed to play $soundId, disabling sounds');
        enabled = false;
        sound.stop();
      }
    }
    

    The key is to call FlxG.sound.update(0), because sound.play() will happily continue even if there is an error in the AL library. The crash will happen later, during the update. Calling the update here does not seem to cause adverse effects. Calling sound.stop() ensures that from that point on the game loop calling FlxG.sound.update() will not cause a crash.

    If anyone knows a cleaner way to do this please let me know.



  • One last detail: this was useful on Windows. On Linux, audio will work even if there is no speaker plugged in.


Log in to reply