Ula*_*ach 5 javascript youtube
嵌入的 youtube iframe 是否可以“加载”视频?
我想在我选择的音乐视频开始后才开始我的脚本。
我已将 onload 事件粘贴到传送 youtube 视频的 iframe 上,但这与实际视频的加载(缓冲已完成准备播放)不符。它仅对应于视频播放器已加载到页面中的时间。
换句话说,有什么方法可以在 youtube 视频开始播放时使用 javascript 进行检测?
您要查找的所有内容都可以在Youtube Iframe API 参考中找到。
我也会将相关代码粘贴到此处,但请注意,我只修改了一行来触发 onReady 警报。其余代码来自上述参考页面。
<!DOCTYPE html>
<html>
<body>
<!-- 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');
tag.src = "https://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: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
alert("Video Ready!");
event.target.playVideo(); // You can omit this to prevent the video starting as soon as it loads.
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9035 次 |
| 最近记录: |