最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Loop option doesn't work in the youtube js api - Stack Overflow

programmeradmin3浏览0评论

I'm wondering why my code doesn't play the loop for the video. Everything is working except the loop option. And I really need it. Thanks a lot.

<script src="jsapi.js"></script>
<script src="swfobject.js"></script>
<div id="ytapiplayer">You need Flash player 8+ and JavaScript enabled to view this video.</div>
<script type="text/javascript">
    google.load("swfobject", "2.1");
    function onYouTubePlayerReady(playerId) {
        ytplayer = document.getElementById("myytplayer");
        ytplayer.playVideo();
        ytplayer.mute();
    // I've tried it, just to.. try hehe  ytplayer.setLoop(true);
    }
    var params = { allowScriptAccess: "always" };
    var atts = { id: "myytplayer" };
    swfobject.embedSWF(";loop=1&enablejsapi=1&playerapiid=ytplayer&allowFullScreen=true&version=3&controls=0&showinfo=0&autohide=1&rel=0",
    "ytapiplayer", "100%", "100%", "8", null, null, params, atts);
</script>

I'm wondering why my code doesn't play the loop for the video. Everything is working except the loop option. And I really need it. Thanks a lot.

<script src="jsapi.js"></script>
<script src="swfobject.js"></script>
<div id="ytapiplayer">You need Flash player 8+ and JavaScript enabled to view this video.</div>
<script type="text/javascript">
    google.load("swfobject", "2.1");
    function onYouTubePlayerReady(playerId) {
        ytplayer = document.getElementById("myytplayer");
        ytplayer.playVideo();
        ytplayer.mute();
    // I've tried it, just to.. try hehe  ytplayer.setLoop(true);
    }
    var params = { allowScriptAccess: "always" };
    var atts = { id: "myytplayer" };
    swfobject.embedSWF("http://www.youtube./v/RLOQCqGKVt8?autoplay=1&loop=1&enablejsapi=1&playerapiid=ytplayer&allowFullScreen=true&version=3&controls=0&showinfo=0&autohide=1&rel=0",
    "ytapiplayer", "100%", "100%", "8", null, null, params, atts);
</script>
Share Improve this question asked Nov 5, 2013 at 17:24 allumetteallumette 1951 silver badge13 bronze badges 1
  • 1 Does this answer your question? Youtube player api with loop – Cees Timmerman Commented Aug 8, 2022 at 8:24
Add a ment  | 

1 Answer 1

Reset to default 15

The solution is to adding in the 'playerVars' object, the 'playlist' attributes. Like this loop works, also for single video.

function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
        height: '100%',
        width: '100%',
        playerVars : {
                  'autoplay' : 1,
                  'rel' : 0,
                  'showinfo' : 0,
                  'showsearch' : 0,
                  'controls' : 0,
                  'loop' : 1,
                  'enablejsapi' : 1,
                  'playlist': 'your-single-video-ID'
                },
        videoId: 'your-single-video-ID',
        events: {
            'onReady': onPlayerReady
            }
       });
}
发布评论

评论列表(0)

  1. 暂无评论