需要将YouTube视频加载到单独的HTML页面中

时间:2018-03-26 08:15:04

标签: javascript youtube youtube-api

我已经在我的主页中加载了播放列表项目,如下所示:

HTML:

<div id="youtube-playlist-feed_1"></div>
<div id="youtube-playlist-feed_2"></div>
<div id="youtube-playlist-feed_3"></div>

加载播放列表的我的脚本是:

            var htmlString = "";
            var apiKey = 'AIzaSyDI4rWo_wVAxRZEIgF6_8sRZDj8OCOZZ38';
            var playlistID = 'PLBhKKjnUR0XBVrHvtDOylN5XREHh9X1nt';
            var maxResults = 7;

            var playlists = [{
                playlistId: 'PLJYHm_ZxWCKnQmapkDs7x47jkr-nw3l50',
                el: '#youtube-playlist-feed_1'
              },
              {
                playlistId: 'PLJYHm_ZxWCKmIBkoopKFK4kTTOmC1Zof0',
                el: '#youtube-playlist-feed_2'
              },
              {
                playlistId: 'PLBhKKjnUR0XAM2Wvi7JY5gLRpFLzIE-An',
                el: '#youtube-playlist-feed_3'
              }
            ];

            playlists.forEach(function(playlist) {
              getVideoFeedByPlaylistId(playlist.playlistId, playlist.el);
            })

            function getVideoFeedByPlaylistId(playlistId, el) {
              $.getJSON('https://www.googleapis.com/youtube/v3/playlistItems?key=' + apiKey + '&playlistId=' + playlistId + '&part=snippet&maxResults=' + (maxResults > 50 ? 50 : maxResults), function(data) {
                $.each(data.items, function(i, item) {
                  var videoID = item['snippet']['resourceId']['videoId'];
                  var title = item['snippet']['title'];
                  var videoURL = 'https://www.youtube.com/watch?v=' + videoID + '&list=' + playlistID + '&index=1';
                  htmlString += '<div class="video-wrap"><div class="video"><a target="_blank" href="' + videoURL + '"><img src="https://i.ytimg.com/vi/' + videoID + '/mqdefault.jpg"></a></div>' + '<div class="title"><a target="_blank" href="' + videoURL + '">' + title + '</a></div></div>';
                });
                $(el).html(htmlString);

                htmlString = '';
              });
            }

现在,我有一个单独的页面,即Player.html,我希望有一个YouTube播放器并加载视频。例如,如果用户点击播放列表的第一项,那么应该在Player.html中加载我的YouTube播放器。我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:0)

首先在VideoID中传递a href,将其写为a href

    <div class="video-wrap"><div class="video">
<a target="_blank" href="player.php?id=' + videoID+ '">
<img src="https://i.ytimg.com/vi/' + videoID + '/mqdefault.jpg"></a>
</div>' + '<div class="title"><a target="_blank" href="player.php?id=' + videoID+ '">' + title + '</a></div></div>

现在在Player.php

使用GET方法获取您的id变量 像:$id = $_GET['id']; 在此文件中使用此代码播放视频

<iframe width="560" height="315" src="https://www.youtube.com/embed/<?php echo $id; ?>" frameborder="0" allowfullscreen></iframe>

答案 1 :(得分:0)

在显示html文件的视频中 你必须写这样的东西:

<a onclick="player()" >Watch</a>

<script type="text/javascript">
    function player() {

         videoid = 'a9Vh9TZkdSM'; //Your Video id

  document.location.href = 'player.html?vid='+videoid;

}
</script>

并在

Player.html

这样的事情:

<div id="display"></div>

<script type="text/javascript">
    window.onload = function () {
    var url = document.location.href,
        params = url.split('?')[1].split('&'),
        data = {}, tmp;
    for (var i = 0, l = params.length; i < l; i++) {
         tmp = params[i].split('=');
         data[tmp[0]] = tmp[1];
    }
     var videoId = data.vid;
     var output = '<iframe width="420" height="315" src="https://www.youtube.com/embed/tgbNymZ7vqY"></iframe>';
     document.getElementById('display').innerHTML = output;
}
</script>

答案 2 :(得分:0)

在您要加载播放列表项目的脚本中,将 htmlString

替换为此代码
 htmlString += '<div class="video-wrap"><div class="video"><a target="_blank" href="#" onclick="player()"><img src="https://i.ytimg.com/vi/' + videoID + '/mqdefault.jpg"></a></div>' + '<div class="title"><a target="_blank" href="#" onclick="player()">' + title + '</a></div></div><input type="text" name="videoid" id="videoid" value="'+videoID+'" hidden>';

Index.html 文件的末尾:

过去这段代码:

<script type="text/javascript">
    function player() {

        var ID = document.getElementById('videoid').value;

  document.location.href = 'player.html?vid='+ID;

}
</script>

并在您的player.html文件中

<div id="display"></div>

<script type="text/javascript">
    window.onload = function () {
    var url = document.location.href,
        params = url.split('?')[1].split('&'),
        data = {}, tmp;
    for (var i = 0, l = params.length; i < l; i++) {
         tmp = params[i].split('=');
         data[tmp[0]] = tmp[1];
    }
     var videoId = data.vid;
     var output = '<iframe width="420" height="315" src="https://www.youtube.com/embed/'+videoId+'"></iframe>';
     document.getElementById('display').innerHTML = output;
}
</script>