Friday, 16 September 2016

How to Dynamically change Youtube Player video

How to Dynamically change Youtube Player videoID



<script src="https://www.youtube.com/iframe_api" type="text/javascript"></script>
<script type="text/javascript">
    /** List of youtube videos */
    var videos = new Array('tGiJSnwBwDQ', 'NjPCjCCjMH4', 'ioWkx6WRH2I')

    var currentId = -1;
    /** Create player object **/
    var player;
    function onYouTubeIframeAPIReady()
    {
        player = new YT.Player('youtubePlayerId', {events: {onReady: onPlayerReady, onStateChange: onPlayerStateChange}})
    }
    function onPlayerReady(event) {
        /** Mute the video **/
        player.mute();
        player.setVolume(0);
        /** Mute the video **/
        //player.playVideo();
    }
    function onPlayerStateChange(event) {
        /* If video stop */
        if (event.data === 0) {
            currentId++;
            if (videos.length <= (currentId + 1)) {
                currentId = 0;
            }
            player.loadVideoById(videos[currentId]);
        }
        /* If video stop */
    }
</script>
<iframe frameborder="0" height="350" id="youtubePlayerId" src="//www.youtube.com/embed/0Bmhjf0rKe8?rel=0&amp;modestbranding=1&amp;autoplay=0&amp;enablejsapi=1" type="text/html" width="390"></iframe>