Home > Article > Web Front-end > html5 method pause() to stop (pause) the currently playing audio or video
Example
A video with play and pause buttons:
var myVideo=document.getElementById("video1"); function playVid() { myVideo.play(); } function pauseVid() { myVideo.pause(); }
Definition and usage
pause( ) method stops (pauses) the currently playing audio or video.
Browser support
All major browsers support the pause() method.
Note: Internet Explorer 8 and earlier versions do not support this method.
Syntax
audio|video.pause()
If we want to use js to control the pause and playback of audio, we cannot directly add click events on the audio, we need to add another button to bind it Click event.
HTML code is as follows:
<button onclick="playPause()">播放/暂停</button> <audio id="audio1" width="420" > <source src="example.mp4" type="audio/mp4" /> <source src="example.ogg" type="audio/ogg" /> </audio>
JS code is as follows:
var myAudio = document.getElementById('audio1'); function playPause(){ if(myAudio.paused){ myAudio.play(); }else{ myAudio.pause(); } }
The above is the detailed content of html5 method pause() to stop (pause) the currently playing audio or video. For more information, please follow other related articles on the PHP Chinese website!