我想知道你们是否都可以帮我翻转这个html5视频播放器的静音/取消静音功能。基本上功能很简单:在播放视频时,它是静音的。当人在视频上按下鼠标时,音频取消静音。当玩家鼠标离开视频时,它再次静音。请记住,我确实希望淡入淡出效果仍然有效。
我已经走到了这一步,但我不知道如何反转静音/取消静音功能。下面是代码。对于实时演示目的,这里是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>
提前谢谢!
答案 0 :(得分:1)
喜欢这个?正如您现在在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 -->
答案 1 :(得分:0)
请使用hover()
。您在问题中描述的内容比mouseover()
和mouseout()
更能使用此功能。
由于视频设置为autoplay
,因此您必须设置视频的初始音量。
您需要直接在DOM元素上设置音量。 volume
属性不是属性,因此不能设置为:
backAudio.attr('volume', 0);
相反,您必须打开jQuery元素并设置volume属性:
jQuery方式:
backAudio.get(0).volume = 0;
普通JavaScript
backAudio[0].volume = 0;
$(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;
}
&#13;
#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;
}
&#13;
<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>
&#13;