Skip to content

allow users to use their own sound class #119

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aaron13100
Copy link

Checklist

This is untested so feel free to reject it. This allows users to override the Sound class, as in the following example.

  class CustomFireworksSound extends Sound {
    play() {
      if (this.isEnabled) {
        var soundFiles = this.options.sound.files;
        var randomIndex = Math.floor(Math.random() * soundFiles.length);
        var gainDifference = Math.abs(this.options.sound.volume.min - this.options.sound.volume.max);

        var gain = Math.random() * gainDifference;
        var fileToPlay = soundFiles[randomIndex];
        var audioElement = document.createElement('audio');
        audioElement.src = fileToPlay;
        audioElement.setAttribute('data-gain', gain);
        audioElement.setAttribute('data-allow-simultaneous', 'true');
        playSoundWithEventTracking(audioElement);
        console.log("custom play function");
      }
    }
  }
  fireworksConfig.soundClass = new CustomFireworksSound(fireworksConfig);

or if we've wrapped all the Fireworks stuff like this..

function fireworksModule() {
   // ... existing code ...
   return {
     Fireworks: Fireworks,
     Sound: Sound
   }
}

Then we can use our custom class like this...

  const fireworksModuleInstance = fireworksModule();
  const FireworksSoundClass = fireworksModuleInstance.Sound;
  class CustomFireworksSound extends FireworksSoundClass {
    play() {
      // ...etc ...
    }
  }
  fireworksConfig.soundClass = new CustomFireworksSound(fireworksConfig);

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant