How to set volume of audio object?

Asked
Active3 hr before
Viewed126 times

7 Answers

objectaudio
90%

Meta Stack Overflow , GitLab launches Collective on Stack Overflow ,Stack Overflow en español, Stack Overflow Public questions & answers

It's not a function, it's a property called volume.

audio.volume = 0.2;
load more v
88%

The HTMLMediaElement.volume property sets the volume at which the media will be played. ,HTMLTableSectionElement,HTMLTableCaptionElement,HTMLMediaElement.muted

var volume = video.volume; //1
load more v
72%

The HTML DOM Audio volume property is used for set or return the current volume of the Audio element. The volume value 0.0 represents silent and 1.0 represents loudest.,Return value: It returns a numeric value which represents the current volume of the Audio element.,It returns the volume property.audio.volume It sets the volume property. audio.volume = number,Below program illustrates the Audio Volume property in HTML DOM:

audio.volume
It sets the volume property.
audio.volume = number
audio.volume = number
load more v
65%

//sets volume to 20%
var vid = document.getElementById("myVideo");
vid.volume = 0.2;
75%

To adjust the volume of your audio element, trigger this short JavaScript snippet which targets your playing audio element (.5 is 50%,You can play multiple streams of audio at once (since iOS 6+),A post was split to a new topic: Creating volume slider for Hype audio element,To pause playing audio, or play paused audio, use the following function:

Your audio element would appear as:

<audio width="400" controls="controls" preload="auto" id="myaudio">
   <source src="https://site.com/audio.mp3" type="audio/mpeg"> <!-- Safari and iPhone -->
</audio>

Any file added to your resource library can be referenced from within your Tumult Hype document using the following variable: ${resourcesFolderName}. If you had a mp3 & ogg file in your Tumult Hype document, and you wanted to create an audio element, you would use the following audio element code:

<audio id="myAudio"width="400" controls="controls" preload="auto">
	<source src="${resourcesFolderName}/audio.mp3"type="audio/mpeg"><!-- Safari and iPhone -->
	<source src="${resourcesFolderName}/audio.ogg"type="audio/ogg"><!-- Firefox and Chrome -->
</audio>

Please note that if you are playing audio that is not externally hosted, you can follow the instructions here. All you need to do is create a small snippet of Javascript which will run when you choose. First, add an ID to your audio element like the text 'lionroar' below:

<audio controls="controls" height="50" id="lionroar" preload="auto" width="300">
   <source src="AUDIO.MP3" type="audio/mpeg">
   </source>
</audio>

Next, create a new javascript that runs 'On Scene Load' and paste in the following code:

var myAudio = document.getElementById("lionroar");
myAudio.play();

What this code does is finds the element with an id 'lionroar' and plays it. To pause it with another user action, you'll run:

var myAudio = document.getElementById("lionroar");
myAudio.pause();

If you wanted to start a timeline, then play the audio, you would run this:

HYPE.documents['MYDOCUMENTNAME'].playTimelineNamed('TIMELINENAME');
var myAudio = document.getElementById('lionroar');
myAudio.play();

To control audio using the 'currentTime' function, you'll need to add your audio element to your Tumult Hype document based on the instructions above (make sure your element IDs match up). To jump to 30 seconds and play, use the following code:

var myAudio = document.getElementById("lionroar");
myAudio.currentTime = 30000;
myAudio.play();

To pause playing audio, or play paused audio, use the following function:

var myAudio = document.getElementById("myAudio");

if (myAudio.paused) {
   myAudio.play();
} else {
   myAudio.pause();
}

To adjust the volume of your audio element, trigger this short JavaScript snippet which targets your playing audio element (.5 is 50%

hypeDocument.getElementById('myaudio').volume = .5;
load more v
40%

If this is your site, make sure that the filename case matches the URL. For root URLs (like http://example.com/) you must provide an index.html file.

The site configured at this address does not contain the requested file.

If this is your site, make sure that the filename case matches the URL.
For root URLs (like http://example.com/) you must provide an index.html file.

http: //example.com/
load more v
22%

true or false. Setting this to true silences the audio object regardless of volume settings,true or false. Set when an audio object has played through its entire duration.,The total length, in seconds, of the sound represented by the audio object.,URL to the source file for the audio object.

<!doctype html>
<html lang="en">

<head>
   <meta charset="UTF-8">
   <title>CH7EX1: Basic HTML5 Audio</title>
</head>

<body>
   <div>
      <audio src="song1.ogg" controls>
         Your browser does not support the audio element.
      </audio>
   </div>
</body>

</html>
load more v

Other "object-audio" queries related to "How to set volume of audio object?"