react & youtube

react & youtube

https://www.npmjs.com/package/react-youtube

https://developers.google.com/youtube/iframe_api_reference

通过 IFrame Player API,您可以在自己的网站上嵌入 YouTube 视频播放器并使用 JavaScript 控制播放器。

与 Flash 和 JavaScript Player API 不同的是,这两者都要求在您的网页上嵌入一个 Flash 对象,而 IFrame API 会将内容发布到网页上的<iframe>标记中。

这种方法比之前提供的 API 更加灵活,因为它允许 YouTube 针对不支持 Flash 的移动设备提供 HTML5 播放器(而非 Flash 播放器)。

demo


<!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: '360',
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();
}
</script>
</body>
</html>
上一篇:WebView cookies清理


下一篇:Hadoop完全分布式环境搭建(二)——基于Ubuntu16.04设置免密登录