P
P
pitimirov_a2015-07-05 02:42:19
YouTube
pitimirov_a, 2015-07-05 02:42:19

How to use yt api on a page twice?

There is a code:

<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: '494', // Высота
          width: '878', // Ширина
          videoId: "<?php echo $video['popup']; ?>", /* kaR07DuN2T8 */ // ID видео

  playerVars: { 'autoplay': 1, 'loop': 1},events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange}});}function onPlayerReady(event) {event.target.playVideo();this.id="pl";} function onPlayerStateChange(event) {if (event.data == YT.PlayerState.ENDED ) {  event.target.playVideo();}}</script>

The problem is that php code can call this fragment several times on the page. However, in this case, only the first video will be displayed in the browser.
I tried to solve the problem in this way:
<?php if(!isset($lal)){$lal=0;}?>
<div id="player<?php print $lal;?>"></div>

  <script>var tag = document.createElement('script');tag.src = "https://www.youtube.com/iframe_api";var firstScriptTag = document.getElementsByTagName('script')[<?php print $lal;?>];firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);var player;function onYouTubeIframeAPIReady() {player = new YT.Player('player<?php print $lal;?>', {
    
          height: '494', // Высота
          width: '878', // Ширина
          videoId: "kaR07DuN2T8", /* kaR07DuN2T8 */ // ID видео

  playerVars: { 'autoplay': 1, 'loop': 1},events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange}});}function onPlayerReady(event) {event.target.playVideo();event.target.setVolume(0);this.id="pl";} function onPlayerStateChange(event) {if (event.data == YT.PlayerState.ENDED ) {  event.target.playVideo();}}</script>

<?php $lal++;?>

However, that didn't help either. What can be done or corrected here? Thanks in advance

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question