如何混合使用 javascript 和流星以使用 youtube api
How to mix javascript and meteor to use youtube api
我正在尝试实现此处描述的示例 youtube api html 页面:https://developers.google.com/youtube/iframe_api_reference 在流星应用程序中。
我了解到您可以使用 Template.<template name>.rendered
在 meteor 应用程序中实现传统的 javascript 功能。
所以我试图通过将它放入渲染函数中来在 meteor 中实现那个 youtube 示例。
但是不会显示任何视频。
我担心我不了解流星的功能。在 meteor 中甚至可能出现这样的情况吗?
代码:
home.html:
enter code here
<template name="home">
<h1> Home</h1>
This is the home page
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>
<script>
</script>
</template>
home.js:
Template.home.rendered = function() {
// 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) {
event.target.playVideo();
}
// 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();
}
}
P.s。我知道 adrianliaw:youtube-iframe-api 并且不想使用它。我想更好地了解如何自己实现它。希望这样做能加深我对 javascript 和流星的了解。
我认为这里的问题是在 https://www.youtube.com/iframe_api
加载后,它会尝试调用 onYouTubeIframeAPIReady
函数但找不到它。如果您将函数更改为存储在整个应用程序可用的变量中的匿名函数,则该代码有效。
home.html:
<head>
<title>test</title>
</head>
<body>
{{> home}}
</body>
<template name="home">
<h1> Home</h1>
This is the home page
<div id="player"></div>
</template>
home.js:
Template.home.rendered = function() {
/* 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;
onYouTubeIframeAPIReady = function() {
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. */
onPlayerReady = function(event) {
event.target.playVideo();
};
/* 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;
onPlayerStateChange = function(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
};
stopVideo = function() {
player.stopVideo();
};
};
注意函数声明的变化:
function onYouTubeIframeAPIReady()
已更改为
onYouTubeIframeAPIReady = function()
function onPlayerReady(event)
已更改为 onPlayerReady
= function(event)
function onPlayerStateChange(event)
已更改为
onPlayerStateChange = function(event)
function stopVideo()
已更改为 stopVideo = function()
这些全局变量现在可由注入的 YouTube 脚本调用。
我正在尝试实现此处描述的示例 youtube api html 页面:https://developers.google.com/youtube/iframe_api_reference 在流星应用程序中。
我了解到您可以使用 Template.<template name>.rendered
在 meteor 应用程序中实现传统的 javascript 功能。
所以我试图通过将它放入渲染函数中来在 meteor 中实现那个 youtube 示例。
但是不会显示任何视频。
我担心我不了解流星的功能。在 meteor 中甚至可能出现这样的情况吗?
代码:
home.html:
enter code here
<template name="home">
<h1> Home</h1>
This is the home page
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>
<script>
</script>
</template>
home.js:
Template.home.rendered = function() {
// 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) {
event.target.playVideo();
}
// 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();
}
}
P.s。我知道 adrianliaw:youtube-iframe-api 并且不想使用它。我想更好地了解如何自己实现它。希望这样做能加深我对 javascript 和流星的了解。
我认为这里的问题是在 https://www.youtube.com/iframe_api
加载后,它会尝试调用 onYouTubeIframeAPIReady
函数但找不到它。如果您将函数更改为存储在整个应用程序可用的变量中的匿名函数,则该代码有效。
home.html:
<head>
<title>test</title>
</head>
<body>
{{> home}}
</body>
<template name="home">
<h1> Home</h1>
This is the home page
<div id="player"></div>
</template>
home.js:
Template.home.rendered = function() {
/* 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;
onYouTubeIframeAPIReady = function() {
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. */
onPlayerReady = function(event) {
event.target.playVideo();
};
/* 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;
onPlayerStateChange = function(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
};
stopVideo = function() {
player.stopVideo();
};
};
注意函数声明的变化:
function onYouTubeIframeAPIReady()
已更改为onYouTubeIframeAPIReady = function()
function onPlayerReady(event)
已更改为onPlayerReady = function(event)
function onPlayerStateChange(event)
已更改为onPlayerStateChange = function(event)
function stopVideo()
已更改为stopVideo = function()
这些全局变量现在可由注入的 YouTube 脚本调用。