geek
geek

Reputation: 205

How to onlooad play an audio

I have an audio file audio . on calling playaudio() function it plays.

i want to play audio when window opens.

how do i do it?

window.onload = function(playAudio();)
var audio = new Audio("https://s3.amazonaws.com/audio-experiments/examples/elon_mono.wav");
		
function playAudio() {
audio.volume = 0.5;
    audio.play();
}

Upvotes: 1

Views: 67

Answers (2)

Nelson Owalo
Nelson Owalo

Reputation: 2414

To reduce the volume to half, simply do:

audio.volume = 0.5;

Here is the documentation

To autoplay the audio, just do:

audio.autoplay = true;

The property can either be true or false. See here

Upvotes: 3

Tanguy Bolle
Tanguy Bolle

Reputation: 21

See here : https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/volume

There is a property called volume that you can change like this :

audio.volume = value;

the expected value is of type double

Upvotes: 2

Related Questions