我想通过url嵌入youtube视频,示例=https://gdata.youtube.com/feeds/api/users/estXcrew/uploads?max-results=1
这是我使用的代码块
<div id="player"></div>
<script>
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '200',
width: '200',
videoId: '//video ID Usually goes here',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
event.target.playVideo();
}
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
我从Youtube API网站复制了这段代码,因为我还没有太多的经验。如何用上面的链接替换"videoId"行?
要加载YouTube视频,请替换
//视频ID通常在这里
带有要加载的视频的ID。
YouTube视频示例:http://www.youtube.com/watch?v=xmP7kQdAPhM
现在在视频ID中,我会使用
视频ID:'xmP7kQdAPhM'
或者,如果你使用了一些PHP,你可以试试下面的方法。
<?php
$feedURL = 'http://gdata.youtube.com/feeds/api/users/estXcrew/uploads?max-results=1';
$sxml = simplexml_load_file($feedURL);
$i=0;
foreach ($sxml->entry as $entry) {
$media = $entry->children('media', true);
$watch = (string)$media->group->player->attributes()->url;
$pat = '/=([^"]*?)&/';
$value=$watch ;
preg_match($pat, $value, $matches);
echo "<iframe title='YouTube video player' class='youtube-player' type='text/html'
width='640' height='390' src='http://www.youtube.com/embed/$matches[1]'
frameborder='0' allowFullScreen></iframe>";
?>
<div class="videoitem">
<div class="videotitle">
<h3><a href="<?php echo $watch; ?>" class="watchvideo"><?php echo $media->group->title; ?></a></h3>
<p><?php echo $media->group->description; ?></p>
</div>
</div>
<?php $i++; if($i==3) { echo '<div class="clear small_v_margin"></div>'; $i=0; } } ?>
示例
首先,关于url。请注意,此url表示使用版本2,并且此版本已被弃用。请参阅"受弃用政策约束的YouTube API":https://developers.google.com/youtube/youtube-api-list另外请注意,默认情况下使用版本1,您可能需要为版本2添加:v=2。如果您更喜欢JSON而不是XML中的响应,那么添加:alt=JSON。为了更好地阅读,请添加:prettyprint=true。例如:https://gdata.youtube.com/feeds/api/users/estXcrew/uploads?maxresults=1&v=2&alt=json&prettyprint=true此外,由于在api版本3中使用了JSON,因此您可能更喜欢使用JSON。对于使用api版本3,有必要首先通过注册获得api密钥。要捕获响应,需要使用XMLHttpRequest()或ActiveXObject()将url发送到服务器。查找可能对您有所帮助的(JS或其他类型)代码库,例如解析和编写XML或JSON字符串;或者发送url请求。请参阅以获取jSON解析:在JavaScript中解析jSON?
关于您提供的代码:使用参数。例如,在获得视频ID:之后
<!DOCTYPE html>
<html>
<head>
<!--
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
// Adding a library to send requests to a server ?
</script>
-->
<script>
var myVideoId = "ZY7aRxb-Z1c"; // Set the videoId here.
// insert an iframe
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player; // reference to the player object
var param = { // video-player parameters
height: '200',
width: '200',
videoId: 'xxxxxxxxxxx',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
};
param.videoId = myVideoId // Replace the videoId parameter
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', param); // create the player object
}
function onPlayerReady(event) {
event.target.playVideo(); // start to play
}
var done = false; // flag
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
//setTimeout(stopVideo, 6000); // This will stop playing after 6 seconds
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
</head>
<body>
<div id="player">please wait...</div>
</body>
</html>