最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - If <video> is playing stop <audio> - Stack Overflow

programmeradmin2浏览0评论

On a webpage I'm using a couple of audio-tags for mouse-over sounds and a video-tag for playing a video.

Now, when the video is playing, the audio should be stopped and disabled. The audio gets triggered like this:

$(this).mouseover(function() {
    $('audio#' + $(this).attr('title') + '_sound').trigger('play');
});

Now there should be added something like this, I've tried a couple of thing which none of are working:

$(this).mouseover(function() {
    if($('#video') is NOT playing) {
        $('audio#' + $(this).attr('title') + '_sound').trigger('play');
    }
});

Anyone made something like this before?

Any help is appreciated :-)

Thanks!

On a webpage I'm using a couple of audio-tags for mouse-over sounds and a video-tag for playing a video.

Now, when the video is playing, the audio should be stopped and disabled. The audio gets triggered like this:

$(this).mouseover(function() {
    $('audio#' + $(this).attr('title') + '_sound').trigger('play');
});

Now there should be added something like this, I've tried a couple of thing which none of are working:

$(this).mouseover(function() {
    if($('#video') is NOT playing) {
        $('audio#' + $(this).attr('title') + '_sound').trigger('play');
    }
});

Anyone made something like this before?

Any help is appreciated :-)

Thanks!

Share Improve this question asked May 27, 2011 at 13:22 StefanStefan 411 silver badge2 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

HTML5 has a paused attribute.

var v = document.getElementsByTagName("video")[0];

$(this).mouseover(function() {
    if(v.paused){  // checks to see if video is paused
       $('audio#' + $(this).attr('title') + '_sound').trigger('play');
    }
});

You should register an event listener to be called whenever the video tag's pause signal has been asserted.

var v = document.getElementsByTagName("video")[0];
v.addEventListener("pause", function() { audio.trigger("play"); }, true);
var video = document.getElementById("myVideo");
var music = document.getElementById("myMusic");
var noOverlap = setInterval(
if(myVideo.paused == false){
    myMusic.pause();
}
, 1000);

This will check to see if the two are overlapping every second and by default, play the video and pause the music.

发布评论

评论列表(0)

  1. 暂无评论