Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

youtube player api for list does always play initial video

I want to switch playlist with https://jsfiddle.net/g1xfh7rd/ but it does always reload the same initial playlist. I tried all solutions suggested YouTube Player API list with videos and it didn't work

    <div id="ytplayer"></div>
    <div onClick="loadPlaylist()">load another</div>


    <script async src="https://www.youtube.com/iframe_api"></script>


    <script>
    // 1. ytplayer code: https://developers.google.com/youtube/player_parameters#IFrame_Player_API

    var tag = document.createElement('script');
    tag.src = "https://www.youtube.com/player_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    var player;


    function onYouTubePlayerAPIReady() 
    {
            player = new YT.Player('ytplayer', 
            {
              height: '390',
              width: '640',
              playerVars: 
              {
                listType:'playlist',
                list: 'RDEMe12_MlgO8mGFdeeftZ2nOQ'
              }
            });
    }


    function loadPlaylist() {
      player.loadPlaylist('PLuvQYuQ7n4rAtqY1dDvhts08zBoisI8xA');
    }



     </script>
     
like image 979
user310291 Avatar asked Apr 11 '21 18:04

user310291


People also ask

Can you play videos with YouTube API?

The IFrame player API lets you embed a YouTube video player on your website and control the player using JavaScript. Using the API's JavaScript functions, you can queue videos for playback; play, pause, or stop those videos; adjust the player volume; or retrieve information about the video being played.

What is api iFrame?

Overview. Developers can use the iFrame API to programmatically create and interact with an Embed or with multiple Embeds in the same web app. The iFrame API includes methods that you can use to start playback, change the content rendering in an Embed, or stop playback.

How do I unmute an iFrame video?

Until and unless you have allow="autoplay;" in your iframe tag you wont be able to unmute the video. Add this attribute and further unMute function will work. Show activity on this post. Usually, you can just click the mute button to unmute it.


Video Answer


1 Answers

It works fine, if you use the object syntax of loadPlaylist() and add a player.stopVideo().

 function loadPlaylist() {
   player.stopVideo();
   player.loadPlaylist({ list: 'PLuvQYuQ7n4rAtqY1dDvhts08zBoisI8xA' });
 }
like image 137
Christopher Avatar answered Sep 20 '22 12:09

Christopher