为什么在视频播放完成后,自定义开始和结束时间设置的Youtube嵌入式视频会重新启动?

时间:2016-08-15 01:43:32

标签: video youtube youtube-iframe-api

我把以下链接放在一起,该链接应该只播放8秒的视频然后停止。

https://www.youtube.com/embed/YjmWnwKxOls?start=47&end=55&showinfo=0&modestBranding=1

当我在Chrome和MS Edge中打开网址时,他们都会以同样的方式回复。视频在指定的开始时间开始播放,播放到结束时间,看起来已经完成,它会快速显示播放器中的相关视频,然后从视频的开头自动开始播放。

查看API here,我发现我的网址没有任何问题,玩家应该采用这种方式行事。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

看看我做了什么,它可以帮助你: 在我的代码中有一个带自动启动的播放列表(仅适用于桌面),音乐以随机顺序播放,直到计数器重置。 您需要保存在计算机中并打开它才能看到结果



<div id="number" style="color: red; margin-left: 200px;">asdfsdf</div>
<div id="arr" style="color: red; margin-left: 200px;">asdfsdf</div>

<div id="player"></div>
    <script>
      // 2. This code loads the IFrame Player API code asynchronously.
      var tag = document.createElement('script');

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

      // 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      //iqtdtJq1I_Q?enablejsapi=1&rel=0&autoplay=1&start=40&end=47&playsinline=0'
      //iqtdtJq1I_Q?enablejsapi=1&rel=0&autoplay=1&start=40&end=47&
      var player;
      function onYouTubeIframeAPIReady() {
        
        player = new YT.Player('player', {
          
          /*height: '390',
          width: '640',
          //videoId: 'iqtdtJq1I_Q?enablejsapi=1&rel=0&autoplay=1&start=35&end=37&',
           videoId: '4Uz8gLA6wbQ',
           playerVars: { start: '35',end: '50','autoplay': 1, 'controls': 0 },*/
          events: {
            'onReady': onPlayerReady,
            'onStateChange': function (event) {
                switch (event.data) {
                    case -1:
                        console.log ('unstarted');
                        
                        break;
                    case 0:
                        console.log ('ended');
                        getRandomWithManyExclusions()
                        event.target.cueVideoById({
                          videoId: videoName,
                          startSeconds: videoStart,
                          endSeconds: videoEnd
                        });
                        event.target.playVideo();
                        break;
                    case 1:
                        console.log ('playing');
                        break;
                    case 2:
                        console.log ('paused');
                        break;
                    case 3:
                        console.log ('buffering');
                        break;
                    case 5:
                        console.log ('video cued');
                        break;
                }
            }
          }
        });
      }
      //'onStateChange': onPlayerStateChange
      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        getRandomWithManyExclusions();
        event.target.cueVideoById({
          videoId: videoName,
          startSeconds: videoStart,
          endSeconds: videoEnd
        });
        event.target.playVideo();
      }
      // 5. The API calls this function when the player's state changes.
      //    The function indicates that when playing a video (state=1),
      //    the player should play for six seconds and then stop.
      var done = false;
      function stopVideo() {
        player.stopVideo();
      }
      function playVideo() {
        player.playVideo();
      }
      
  var arrayOfIndexesToExclude = new Array();
  var videoName = 'am1_JLFDFMw';
  var videoStart = '19';
  var videoEnd = '21';

  function getRandomWithManyExclusions(){
    var rand = null;
      
		  	do{
		    	rand = Math.round(Math.random() * ( 9)); //numero aleatorio até 9
		    	if(arrayOfIndexesToExclude.length >= 10){ //10 é o numero de aleatorios totais, incluindo-se 0 
		 			  arrayOfIndexesToExclude.length = 0;	   			
		    	}
				}while(arrayOfIndexesToExclude.includes(rand));
			
			arrayOfIndexesToExclude.push(rand);  
      switch(rand) {
        case 0:
          videoName = '8B-sFGfA3lM'; //WHOAMI
          videoStart = '38';
          videoEnd = '156';      
          break;
        case 1:
          videoName = 'gg5RwFL4F_w';//noise-x
          videoStart = '68';
          videoEnd = '193';          
          break;
        case 2:
          videoName = 'VX56xOe86UM'; //Nightmare God
          videoStart = '0';
          videoEnd = '49';
          break;
        case 3:
          videoName = '2VbKY2miqXo'; //Bloodvault X
          videoStart = '0';
          videoEnd = '57';
          break;
        case 4:
          videoName = 'AiCfXlYx9HI'; //Fukkk Offf
          videoStart = '41';
          videoEnd = '93';
          break;
        case 5:
          videoName = 'k_ZESIw3K0c'; //Cyber Electro
          videoStart = '49';
          videoEnd = '131';
          break;
        case 6:
          videoName = 'kQ6wY_mGuzQ'; //Scum of Mankind
          videoStart = '13';
          videoEnd = '55';
          break;
        case 7:
          videoName = '4Uz8gLA6wbQ'; //Extreme Terror
          videoStart = '35';
          videoEnd = '95';
          break;              
        case 8:
          videoName = 'ZJxX-MIXtXo'; //Terror Of My speedcore 1
          videoStart = '0';
          videoEnd = '59';
          break;
        case 9:
          videoName = 'ZJxX-MIXtXo'; //Terror Of My speedcore 2
          videoStart = '113';
          videoEnd = '134';
          break;
        default:
          break;
      }
      
    
    document.getElementById("number").innerHTML = arrayOfIndexesToExclude;
    document.getElementById("arr").innerHTML = arrayOfIndexesToExclude.length;
  }

</script>
</body>
</html>
              <!--<audio id="musica" autoplay volume="1" loop  >
  <source src="msc/playlist.wav" type="audio/wav"></source>
</audio>
https://www.youtube.com/embed/a1oSXlU0ZUk?enablejsapi=1&rel=0&playsinline=0&autoplay=1



	if (arrayOfIndexesToExclude.length < 9){
      }else{
      arrayOfIndexesToExclude = [];
      getRandomWithManyExclusions();
    }

    switch(rand) {
      case 0: /*extreme terror*/
          document.getElementById['playerId'].src = "http://www.youtube.com/embed/4Uz8gLA6wbQ?enablejsapi=1&rel=0&playsinline=0&autoplay=1&start=35&end=95";
          break;
                       
      default:
          code block
    }
    document.getElementById("number").innerHTML = arrayOfIndexesToExclude;
    arrayOfIndexesToExclude.push(rand);
    
<iframe id="playerId" style="opacity:0;" type="text/html" width="100%" height="%f" src="http://www.youtube.com/embed/iqtdtJq1I_Q?enablejsapi=1&rel=0&playsinline=0&autoplay=1&start=0&end=47" frameborder="0" allowfullscreen>
</iframe>



    -->
&#13;
&#13;
&#13;