joshwcomeau / use-sound

A React Hook for playing sound effects
MIT License
2.75k stars 98 forks source link

play() without argument does not work if sprites are defined #109

Open fabswt opened 2 years ago

fabswt commented 2 years ago

Bug report

If defining sprites on a sound, calling play() without any argument results in an error. Here are examples, make sure to reload the page between two tests.

OK

// test 1 - works fine: plays the whole sound
howl_sound = new Howl({
  src: 'http://tests.localhost/Assets/Audio/A1-004-001.wav',
  format: ['wav'],
});
howl_sound.play();

// test 2 - works fine: plays the sprite
howl_sound = new Howl({
  src: 'http://tests.localhost/Assets/Audio/A1-004-001.wav',
  format: ['wav'],
  sprite: {
    hmm: [100, 450, false],
  },
});
howl_sound.play('hmm');

Not OK

// test 3 - doesn't work: does not play anything
howl_sound = new Howl({
  src: 'http://tests.localhost/Assets/Audio/A1-004-001.wav',
  format: ['wav'],
  sprite: {
    hmm: [100, 450, false],
  },
}).play();

And then, trying to howl_sound.play() again will result in the following error:

Uncaught TypeError: Cannot read properties of undefined (reading '0') at Howl.play (howler.js:826:82)

Ambiguity

I can see how there could be some ambiguity here. If defining a sprite, do we mean to use sprites EXCLUSIVELY? In my use case, I need both sprites and the ability to play parts of the audio (for one and the same piece of audio.)

If play() should always be used with an argument when sprites are defined, then the docs should say so (my apologies if ever they do and I missed it.)

Would make sense to let play() (without argument) work even when sprites are defined.

Version

Tested using https://cdnjs.cloudflare.com/ajax/libs/howler/2.2.3/howler.js