use*_*132 5 javascript embed youtube video mute
如何将 youtube 嵌入式播放器设置为在单击时取消静音。您可以在http://www.harvestarmy.org主页上看到我所指的嵌入式播放器。它是右侧的那个,上面写着“来自 YouTube 的最新视频。我将其设置为默认静音,但我希望它在有人点击时自动取消静音。
这是我使用的代码:
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
// This is a protocol-relative URL as described here:
// http://paulirish.com/2010/the-protocol-relative-url/
// If you're testing a local page accessed via a file:/// URL, please set tag.src to
// "https://www.youtube.com/iframe_api" instead.
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '98',
width: '175',
playerVars: {
'list': 'UUFwY5Al1Doy7f0MdKnJ-gaw',
'modestbranding': 1,
'showinfo': 0,
'autoplay': 1
},
events: {
'onReady': onPlayerReady
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.mute();
}
</script>
Run Code Online (Sandbox Code Playgroud)
谢谢你的回答!我的实现方式略有不同,将其添加到 onYouTubeIframeAPIReady 函数中。
我还反转了 isUnMulated 变量的初始化值。功能上是相同的,但这样更有意义。
var player;
var isUnMuted = false;
window.onYouTubeIframeAPIReady = function() {
player = new YT.Player('youTube', {
videoId: '[myVideoId]',
playerVars: {
'rel': 0,
},
events: {
'onReady': function() {
player.mute();
player.playVideo();
},
'onStateChange': function () {
if (player.isMuted() && player.getPlayerState() == 2 && !isUnMuted) {
player.unMute();
player.playVideo();
isUnMuted = true;
}
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
这与 Facebook 新闻源中嵌入视频的行为非常相似。
小智 1
我认为这是不可能的,因为没有“点击”或“激活”事件之类的东西。但是,您可以尝试向“onStateChange”事件添加事件处理程序,并取消静音和取消暂停播放器。我还没有尝试过,但尝试一下看看是否有效:
var isUnMuted = true;
player.addEventListener("onStateChange", function(event) {
if( player.isMuted() && player.getPlayerState() == 2 && isUnMuted ) {
player.unMute();
player.playVideo(); // resume playback
isUnMuted = false; // you want to run this once only!
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14156 次 |
| 最近记录: |