简体   繁体   English

将这个html5视频播放器的静音/取消静音功能反转

[英]Inverting the mute / unmute function this particular html5 video player

Im wondering if you all can help me Invert the mute / unmute function for this html5 video player. 我想知道是否所有人都可以帮助我反转此html5视频播放器的静音/取消静音功能。 Basically the function is simple: While the video is playing, it is muted. 基本上,该功能很简单:播放视频时,将其静音。 When the person overs their mouse on the video the audio unmutes. 当此人将鼠标悬停在视频上时,音频将保持不变。 When the players mouse leaves the video, it mutes again. 当播放器的鼠标离开视频时,它会再次静音。 Please keep in mind that I do want the fade effect to still function. 请记住,我确实希望淡入淡出效果仍然起作用。

I've gotten this far, but I have no idea how to invert the mute / unmute function. 我已经走了这么远,但是我不知道如何反转静音/取消静音功能。 Below is the code. 下面是代码。 For live-demo purposes, here is the JSFiddle 为了现场演示,这里是JSFiddle

 $(document).ready(function() { var backAudio = $('#mediaplayer'); var muted = false; $('.mute').mouseover(function() { var video = $(this); if (!muted) { video.attr("disabled", ""); backAudio.animate({ volume: 0 }, 1000, function() { muted = true; }); } else { video.attr("disabled", ""); backAudio.animate({ volume: 1 }, 1000, function() { muted = false; }); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <video id="mediaplayer" class="mute" src="http://www18.online-convert.com/download-file/b7124f78114fba6bb5137d36b5429e6e/converted-14cf4fb1.mp4" autoplay="autoplay" volume="0"> </video> 

Thank you in advance! 先感谢您!

Like this? 像这样? As you have it now on the JsFiddle, the audio begins playing immediately. 当您现在在JsFiddle上拥有音频时,音频立即开始播放。

$(document).ready(function(){
var backAudio = $('#mediaplayer');
var muted = true;
backAudio[0].volume = 0;

 $('.mute').hover(function(){
     var video = $(this);
     if (muted) {
         video.attr("disabled", "");
         backAudio.animate({volume: 1}, 1000, function () {
             muted = false;
         });
     }
     else {
         video.attr("disabled", "");
         backAudio.animate({volume: 0}, 1000, function () {
             muted = true;
         });
     }
 }); <!-- END | mute -->
}); <!-- END | mediaplayer -->

http://jsfiddle.net/fdpxw6rm/ http://jsfiddle.net/fdpxw6rm/

Please use hover() for this. 请为此使用hover() What you have described in your question makes the use of this function better than mouseover() and mouseout() . 您在问题中描述的内容使此功能的使用优于mouseover()mouseout()

jQuery & the DOM jQuery和DOM

Since the video is set for autoplay , you must set the initial volume for the video. 由于视频设置为autoplay ,因此您必须设置视频的初始音量。

You need to set the volume directly on the DOM element. 您需要直接在DOM元素上设置音量。 The volume property is not an attribute, so it cannot be set like: volume属性不是属性,因此不能将其设置为:

backAudio.attr('volume', 0);

Instead, you must unwrap the jQuery element and set the volume property: 相反,您必须解开jQuery元素并设置volume属性:

  • jQuery way: jQuery方式:

     backAudio.get(0).volume = 0; 
  • Plain JavaScript 纯JavaScript

     backAudio[0].volume = 0; 

Solution

 $(document).ready(function () { var backAudio = $('#mediaplayer'); var mutedOverlay = $('#muted-wrapper'); backAudio[0].volume = 0; mutedOverlay.show(); $('.mute').hover(function () { var video = $(this); video.attr("disabled", ""); backAudio.animate({ volume: 1 }, 1000, function () { mutedOverlay.hide(); }); }, function () { var video = $(this); video.attr("disabled", ""); backAudio.animate({ volume: 0 }, 1000, function () { mutedOverlay.show(); }); }); }); // NOTE: This is not used, but could be helpful to you in the future. function isPlaying(videoEL) { if (videoEL.currentTime > 0) { return !(videoEL.paused || videoEL.ended); } return false; } 
 #container { width: 480px; height: 360px; position: relative; } #mediaplayer, #muted-wrapper { width: 100%; height: 100%; position: absolute; top: 0; left: 0; } #muted-wrapper { display: table; z-index: 10; color: #FFFFFF; font-weight: bold; font-size: 64px; background-color: rgba(0,0,0,.7); text-shadow: 1px 1px 1px #000000; } #muted-text { display: table-cell; text-align: center; vertical-align: middle; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <div id="container" class="mute"> <video id="mediaplayer" autoplay="autoplay" volume="0" src="http://www18.online-convert.com/download-file/b7124f78114fba6bb5137d36b5429e6e/converted-14cf4fb1.mp4"></video> <div id="muted-wrapper"> <div id="muted-text">Muted</div> </div> </div> 

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

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