The Wayback Machine - https://web.archive.org/web/20240914031746/https://www.geeksforgeeks.org/html-dom-audio-play-method/
Open In App

HTML | DOM Audio play() Method

Last Updated : 14 Apr, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The HTML DOM Audio play() method is used to start playing the current audio. To use the Audio play() method, one must use the controls property to display the audio controls such as play, pause, seeking, volume, etc, attached to the audio. This method is used together with the pause() method.
Syntax: 
 

audioObject.play()

Return Value: 
 

  • No return value

Parameter: 
 

  • No Parameter

Example: 
 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        DOM Audio play( ) Method
    </title>
</head>
 
<body style="text-align:center">
 
    <h1 style="color:green">
    GeeksforGeeks
    </h1>
    <h2 style="font-family: Impact">
    Audio play() method
    </h2>
    <br>
    <audio id="idAudio">
        <source src=
                type="audio/ogg">
        <source src=
                type="audio/mpeg">
      Your browser does not support the audio element.
    </audio>
 
     
<p>To play the Audio, click the "Play Audio" button.</p>
 
 
    <button onclick="play_Audio()" type="button">Play Audio</button>
    <button onclick="pause_Audio()" type="button">Pause Audio</button>
 
    <script>
        var GFG = document.getElementById("idAudio");
 
        function play_Audio() {
            GFG.play();
        }
 
        function pause_Audio() {
            GFG.pause();
        }
    </script>
 
</body>
 
</html>


Output: 
 

Supported Browsers: 
 

  • Chrome
  • Mozilla Firefox
  • Internet Explorer 9.0
  • Opera
  • Safari

 



Similar Reads

How to Load an Audio Buffer & Play it using the Audio Tag ?
Playing audio files on a web page is something people often need when making websites. One way to do this is by loading the audio into a buffer and playing it using the HTML &lt;audio&gt; tag. This method makes it easy to handle and play audio files right in the web browser. ApproachFirstly we select a file input element (fileInput) from the DOM an
2 min read
How to Play Single Audio Element at a time in HTML ?
To play a single audio element at a time in HTML, use the 'audio' tag and JavaScript. Assign each audio element a unique ID. When playing a new audio, check if any audio is currently playing and pause it before starting the new one. To play a single audio element at a time in HTML using JavaScript, you can create a function that controls the playba
2 min read
How to record and play audio in JavaScript ?
JavaScript is a very flexible language and it provides many API support to do many useful things. Here one important thing is that record audio or video in web pages is also done using JavaScript. In this case, it will ask the user for microphone access to the browser and record the audio through the microphone and save the audio data chunks in for
4 min read
How to play audio repeatedly using HTML5 ?
This article will show you how an audio file can be played repeatedly on a web page. This is done by using the loop attribute of the &lt;audio&gt; tag. It is used to restart the audio again and again after loading the web page. This can be used in situations where the audio has to be looped until it is specifically stopped, like in the case of back
1 min read
How to catch an audio DOMException caused by a file that cannot play ?
In this article, we will try to understand the fact that How to catch an audio DOMException caused by a file that cannot be played. When trying to play an audio file, a DOMException may be thrown if the file is not supported or cannot be played. For example, if the file is not in a supported format or is corrupted. It is important to handle these e
3 min read
How to toggle play/pause in ReactJS with audio ?
In this article, we will learn to create a play/pause button for an audio file using ReactJS. Prerequisites:NodeJS or NPMReact JSApproach: Take the reference of the audio file in ReactJS Using Audio Class Set the default state of the song as not playing.Make a function to handle the Play/Pause of the song.Use the play() and pause() functions of the
2 min read
How to make Sound only Play Once in HTML5 Audio ?
In HTML5, we can validate the audio playing by using the proper event handling and control manipulation. In this article, we will explore two different approaches to create sound that only plays once in HTML5 Audio. Table of Content Using JavaScript Event ListenersUsing onended AttributeUsing JavaScript Event ListenersIn this approach, we are using
2 min read
How to Play/Pause video or Audio in camera application in ReactJS ?
In this article, we'll construct a basic Respond application that permits clients to control their video and sound transfers with on/off buttons. We'll utilize symbols to address the video and sound controls for a more easy-to-use interface. This can be valuable for applications like video conferencing, where clients need the adaptability to turn t
4 min read
How to Play/Pause video or Audio in ReactJS ?
Adding multimedia elements, such as videos and audios, to your ReactJS applications can enhance the overall user experience. One common requirement is to provide controls for playing and pausing these media files. In this article, we'll explore how to implement play/pause functionality for videos and audios in ReactJS. In this article, we will disc
3 min read
HTML DOM Video play( ) Method
The Video play() method is used to start playing the current video. To use the Video play() method, one must use the controls property to display the video controls such as play, pause, seeking, volume, etc, attached to the video. Syntax: videoObject.play() Note: The Video play() method does not accept any parameters and does not return any values.
1 min read
How to mute the audio using amp-audio of Google AMP ?
Introduction: Since the release of HTML5, audios can be added to webpages using the “audio” tag. Previously audios could be only played on webpages using web plugins like Flash. The “audio” tag is an inline element which is used to embed sound files into a web page. It is a very useful tag if you want to add audio such as songs, interviews, etc on
2 min read
How to set audio loop in amp-audio of Google AMP ?
Since the release of HTML5, audios can be added to webpages using the “audio” tag. Previously audios could be only played on webpages using web plugins like Flash. The “audio” tag is an inline element which is used to embed sound files into a web page. It is a very useful tag if you want to add audio such as songs, interviews, etc on your webpage.
2 min read
HTML Audio/Video DOM addTextTrack() Method
The HTML Video DOM addTextTrack() Method is used to create or return the new TextTrack of a video Element. The new TextTrack is added to the list of all TextTracks of a Video Element. Syntax: audio/video.addTextTrack(kind, label, language) Parameter Values: Kind: It specifies the Kind of the TextTrack"subtitles""caption""descriptions""chapters""met
1 min read
HTML DOM Audio pause() Method
The HTML DOM Audio pause() Method is used to pause the currently playing audio. To use the audio pause() method, one must use the controls property to display the audio controls such as play, pause, volume, etc, attached to the audio. The audio pause() method does not accept any parameters and does not return any values. Syntax: audio.pause() Param
1 min read
HTML | DOM Audio canPlayType() Method
The Audio canPlayType() method is used for checking if the browser can play the specified audio type or not.Return: The Audio canPlayType() method generally returns one of the following values: “probably”: It means that this audio type is most likely supported for browser.“maybe”: It means that the browser might support this audio type.“” (empty st
1 min read
HTML | DOM Audio load() Method
The Audio load() method is used for reload the audio element. The Audio load() method is used to update the audio element after changing the source or other settings. The Audio load() method does not accept any parameters and does not return any values.Syntax audio.load() Example: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;body style=&quot;t
1 min read
HTML DOM Audio preload Property
The Audio preload property is used for setting or returning the value of the preload attribute of audio. The preload attribute is used to specify the way the author thinks the audio should be loaded when the page loads. The audio preload attribute allows the author to portray to the browser the way the user experience of a website should be impleme
2 min read
HTML DOM Audio readyState Property
The Audio readyState property is used for returning the current ready state of the audio. The ready state is used for indicating if the audio is ready to play or not. The Audio readyState property is a read-only property. The various numbers depicting different ready states are: 0 = HAVE_NOTHING: There is no information related to whether or not th
2 min read
HTML DOM Audio Object
The Audio object is used for representing an HTML &lt;audio&gt; element. The Audio Object is a new object in HTML5. Syntax: For creating an &lt;audio&gt; element:let gfg = document.createElement("AUDIO")For accessing an &lt;audio&gt; element:let x = document.getElementById("myAudio") Property Values: ValueDescriptionaudioTracksIt is used for return
4 min read
HTML DOM Audio audioTracks Property
The Audio audioTracks Property is used for returning an AudioTrackList object. The audio tracks available for a video are represented by the AudioTrackList object. The AudioTrack Object is used to represent an available audio track. Syntax: audioObject.audioTracks Return Values: AudioTrackList Object: It is used to represent the available audio tra
1 min read
HTML DOM Audio autoplay Property
The Audio autoplay property is used for setting or returning whether audio should start playing as soon as it is loaded or not. It can be used to specify that the audio should automatically start playing as soon as it is loaded. Syntax: Return the autoplay property:audioObject.autoplaySet the autoplay property:audioObject.autoplay = true|false Prop
2 min read
HTML DOM Audio buffered Property
The Audio buffered property is used for returning a TimeRanges object. The user’s buffered ranges of the audio can be represented using the TimeRanges object. The time range of buffered audio is defined by the buffered range and if the user skips in the audio, it may result in several buffered ranges. Syntax: audioObject.buffered Return Values: Tim
2 min read
HTML DOM Audio controls Property
The Audio controls property is used for setting or returning whether audio should display standard audio controls or not. The &lt;audio&gt; controls attribute is reflected by this property. The audio controls included in the property are: PlayPauseSeekingVolume Syntax: Return the control property:audioObject.controlsSet the control property:audioOb
1 min read
HTML DOM Audio currentSrc Property
The Audio currentSrc property is used for returning the URL of the current audio. The Audio currentSrc property returns an empty string if no audio is specified. The Audio currentSrc property is a read-only property. Syntax: audioObject.currentSrc The below program illustrates the Audio currentSrc Property: Example: Getting the URL of the current a
1 min read
HTML DOM Audio currentTime Property
The Audio currentTime property is used for setting or returning the current position of the audio playback. The Audio currentTime property returns the audio playback position in the form of seconds. The playback jumps to the specified position when this property is set. Syntax: Return the currentTime property:audioObject.currentTimeSet the currentT
2 min read
HTML DOM Audio defaultMuted Property
The Audio defaultMuted property is used for setting or returning whether the audio should be muted by default or not. The audio defaultMuted property cannot change the current mute state, it only affects the default muted state. Syntax: Return the deafaultMuted property:audioObject.defaultMutedSet the defaultMuted property:audioObject.defaultMuted
2 min read
HTML DOM Audio defaultPlaybackRate Property
The Audio defaultPlaybackRate property is used for setting or returning the default playback speed of the audio. The Audio defaultPlaybackRate property only changes the default playback speed, not the current playback speed. Syntax: Return the defaultPlaybackRate property:audioObject.defaultPlaybackRateSet the defaultPlaybackRate property:audioObje
2 min read
HTML DOM Audio duration Property
The Audio duration property is used for returning the length of audio. The Audio duration property returns the value in seconds. Different browsers return different precision values such as safari returning up to 14 decimal places followed by Opera returning up to 9 decimal places. The Audio duration property is a read-only property. The Audio dura
2 min read
HTML DOM Audio ended Property
The Audio ended property is used for returning whether the playback of the audio has ended or not. When the playback position is at the end of the audio, we consider the audio has ended. The Audio-ended property returns a boolean true if the audio has ended, else it returns false. The Audio ended property is a read-only property. Syntax: audioObjec
1 min read
HTML DOM Audio loop Property
The Audio loop property is used for setting or returning whether an audio should start playing over again when it is finished or not. Syntax: Return the loop property:audioObject.loopSet the loop property:audioObject.loop = true | false Property Values: true | false: It is used to specify whether the audio should start playing over again, every tim
2 min read
Article Tags :