Home  >  Article  >  Web Front-end  >  html5 method pause() to stop (pause) the currently playing audio or video

html5 method pause() to stop (pause) the currently playing audio or video

黄舟
黄舟Original
2017-11-08 09:29:1411567browse

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(&#39;audio1&#39;);
    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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn