简体   繁体   中英

How to run a function when a html5 video starts playing

i am building an app with ionic, i want to run a function whenever a video starts to play,like hide a button and etc. here's my code

    let video = <HTMLVideoElement> document.getElementById(i)
    if(video.paused) {
        video.play()
        video.oncanplay=()=>console.log('Starts playing');
    }

The code is not working, please how can i run a function whenever any video on that page starts playing

var vid = document.getElementById("myVideo");
vid.onplay = function() {
    alert("The video has started to play");
};

From here: https://www.w3schools.com/tags/av_event_play.asp

Try this, play returns a promise.

const video = document.getElementById("myVideo");

async function playVideo() {
  try {
    await video.play();
    // video is playing, do your stuff here
  } catch(err) {
    // video is not playing
  }
}

if(video.paused) {
    playVideo()
 }

For more info: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/play

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM