javascripthowler.js

Check if any audio is playing with JS


Is it possible to check if any audio is playing with JS?

I know, I could have global variables for each sound and go through all of them with the answer provided here: HTML5 check if audio is playing?

I guess, it's not possible, because also HowlerJS does not provide such a thing. Or is there a way?


Solution

  • You can still use a similar approach to check if any audio is playing. You'll need to iterate through the Howler.js sound objects and check their playing() method:

    function isAudioPlaying() {
      var sounds = Howler._howls;
    
      for (var i = 0; i < sounds.length; i++) {
        if (sounds[i].playing()) {
          return true;
        }
      }
    
      return false;
    }
    

    In this case, the Howler._howls array contains all the active Howler.js sound objects, and you can use the playing() method to check if each sound is currently playing. Keep in mind that the approach of checking the paused property or using playing() method will determine if audio is currently playing, but it might not be accurate for audio that is in the process of loading or buffering.

    It's important to note that _howls is an internal property, which means it's not part of the official public API of Howler.js. This means that you shouldn't rely on it directly in your code, as it could change or be removed in future versions of the library. If you need to keep track of multiple sound instances in your application, you can maintain your own data structures or arrays to manage these instances as needed. By registering it during creation.