Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Web Technology by (20.3k points)

I am making a game with HTML5 and Javascript.

How could I play game audio via Javascript?

1 Answer

0 votes
by (40.7k 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>

Related questions

Browse Categories

...