0 votes
1 view
in Web Technology by (22.4k points)

I am making a game with HTML5 and Javascript.

How could I play game audio via Javascript?

1 Answer

0 votes
by (40.3k points)

Use the code given below if you don't want to use HTML elements:

var audio = new Audio('audio_file.mp3');

audio.play();

var audio = new Audio('

audio.play();

 This uses the HTMLAudioElement interface, which plays audio the same way as the <audio> element.

If you want more functionality, use the howler.js library as it's simple and useful.

<script src="https://cdnjs.cloudflare.com/ajax/libs/howler/2.1.1/howler.min.js"></script>

<script>

    var sound = new Howl({

      src: ['

      volume: 0.5,

      onend: function () {

        alert('Finished!');

      }

    });

    sound.play()

</script>

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...