Detect if HTML5 Video element is playing

后端 未结 16 2443
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 00:43

I\'ve looked through a couple of questions to find out if an HTML5 element is playing, but can\'t find the answer. I\'ve looked at the W3 documentation and it has an event n

相关标签:
16条回答
  • 2020-11-29 01:23

    This is my code - by calling the function play() the video plays or pauses and the button image is changed.

    By calling the function volume() the volume is turned on/off and the button image also changes.

    function play() { 
      var video = document.getElementById('slidevideo'); 
      if (video.paused) {
        video.play()
        play_img.src = 'img/pause.png'; 
      }
      else {
        video.pause()
        play_img.src = 'img/play.png';
      }
    }
    
    function volume() { 
      var video = document.getElementById('slidevideo');
      var img = document.getElementById('volume_img');
      if (video.volume > 0) {
        video.volume = 0
        volume_img.src = 'img/volume_off.png';  
      }
      else {
        video.volume = 1
        volume_img.src = 'img/volume_on.png';
      }
    }
    
    0 讨论(0)
  • 2020-11-29 01:23

    I just added that to the media object manually

    let media = document.querySelector('.my-video');
    media.isplaying = false;
    

    ...

    if(media.isplaying) //do something
    

    Then just toggle it when i hit play or pause.

    0 讨论(0)
  • 2020-11-29 01:24

    It seems to me like you could just check for !stream.paused.

    0 讨论(0)
  • 2020-11-29 01:24

    a bit example

    var audio = new Audio('https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3')
    
    if (audio.paused) {
      audio.play()
    } else {
      audio.pause()
    }

    0 讨论(0)
提交回复
热议问题