简体   繁体   English

后续访问时将HTML5视频静音

[英]Mute HTML5 video on subsequent visit

I am using Reveal Modal ( http://zurb.com/playground/reveal-modal-plugin ) to fire off a modal pop-up box on the visitor's first visit only , setting a cookie using jQuery Cookie ( https://github.com/carhartl/jquery-cookie ). 我使用的显示模态( http://zurb.com/playground/reveal-modal-plugin )到断火在访问者的首次访问一个模式弹出框,设置使用jQuery曲奇(饼干https://开头的github .com / carhartl / jquery-cookie )。

Here is the code for the modal (shows a GIF on mobile devices): 这是模式代码(在移动设备上显示GIF):

<div id="myModal" class="reveal-modal">
</div>

<script type="text/javascript">
    var isMobile = navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/);
    var myModal = document.getElementById('myModal');

    if(!isMobile) {
    // User-Agent is not IPhone, IPod, IPad, Android or BlackBerry
    myModal.innerHTML += '<video autoplay>' +
    '<source src="video/LogoOpening.mp4" type="video/mp4"/>' +
    '<source src="video/LogoOpening.ogg" type="video/ogg"/>' +
    '<source src="video/LogoOpening.webm" type="video/webm"/>' +
    '</video>' +
    '<a class="close-reveal-modal"><div class="button4">Close</div></a>';
    } else {
    myModal.innerHTML += '<img src="images/ThroughTheYears.gif" alt="Logo History" />' +
    '<a class="close-reveal-modal"><div class="button4">Close</div></a>' +
    '</div>';
    }
</script>

...and here is the Javascript that fires off the modal after checking for the cookie: ...这是检查Cookie后触发模式的Javascript:

<script>
    $(document).ready(function() {
    if ($.cookie('modal_shown') == null) {
        $.cookie('modal_shown', 'yes', { expires: 30, path: '/' });
        $('#myModal').reveal({
         animation: 'fade',                         //fade, fadeAndPop, none
         animationspeed: 500,                       //how fast animtions are
         closeonbackgroundclick: true,              //if you click background will modal close?
         dismissmodalclass: 'close-reveal-modal'    //the class of a button or element that will close an open modal
         });
    }
    });
</script>

So, here's the issue: when my visitor shows up the first time, the video fires off perfectly and plays automatically, just like it should (a similar animated GIF plays on mobile devices only) ; 所以,这就是问题所在:当我的访客第一次出现时,视频会完美启动并自动播放,就像应该的那样(类似的GIF动画只能在移动设备上播放) however, the video has sound, and on subsequent visits the video autoplays and you hear the audio, but the modal doesn't visually fire off (the modal and video stays hidden). 但是,视频有声音,随后的访问会自动播放视频,您会听到音频,但是模态不会从视觉上触发(模态和视频保持隐藏)。

I think the solution would be to somehow tie the video's mute attribute to the cookie checking Javascript (which determines whether the modal fires or not), but I'm not sure how to code that. 我认为解决方案将以某种方式将视频的静音属性与Cookie进行检查的Javascript(确定是否触发模式)相关联,但是我不确定如何编写代码。 Help? 救命?

something like this should work 这样的事情应该工作

if (!isMobile) {
    // User-Agent is not IPhone, IPod, IPad, Android or BlackBerry
    if ($.cookie('modal_shown') == null) {
        myModal.innerHTML += '<video autoplay controls>'
    } else {
        myModal.innerHTML += '<video autoplay muted controls>'
    }
    myModal.innerHTML += '<source src="video/LogoOpening.mp4" type="video/mp4"/>' +
    ....
    ....
    '</video>' +

... adding the extra check for the cookie model_shown allows you to change if the video will autoplay, or will autoplay but be muted (if you would prefer it not to autoplay you could remove that, in which case the muted may also not be needed. I also added the controls so the user can control volume or play/pause manually if desired ...添加cookie的额外检查model_shown可以更改视频是否将自动播放,或者将自动播放但被静音(如果您不希望其不自动播放,则可以将其删除,在这种情况下,静音也可能不会被自动播放)我还添加了controls以便用户可以根据需要手动控制音量或播放/暂停

hope this helps (if not quite what you need just comment and I'll try and get closer) 希望这会有所帮助(如果不是您所需要的只是评论,我会尽力与您取得联系)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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