2014-08-31 68 views
2

我想创建一个自定义的youtube progressbar。我做了一些事情,但它没有按照应有的方式工作。我希望它像YouTube的进度条一样平稳运行,不是像我一样每秒更新一次,而是从0到100%,现在我的98%停止。另外,我希望前进行栏在视频暂停时停止,并在再次播放视频时再次运行。Youtube使用YouTube iframe api和jquery不工作的自定义progressbar

进度

function progress(percent, $element) { 
var progressBarWidth = percent * $element.width()/100; 

// $element.find('div').animate({ width: progressBarWidth }, 500).html(percent + "% "); 

$element.find('div').animate({ width: progressBarWidth }); 
} 

进度CSS

#progressBar { 
width: 960px; 
height: 6px; 
background-color: #444444; 
display: none; 
margin-top: 1px; 
} 

#progressBar div { 
height: 100%; 
width: 0; 
background-color: #ffffff; 
} 

YouTube的iframe中API

// Load the IFrame Player API code asynchronously. 
var tag = document.createElement('script'); 
tag.src = "https://www.youtube.com/player_api"; 
var firstScriptTag = document.getElementsByTagName('script')[0]; 
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); 

// Replace the 'ytplayer' element with an <iframe> and 
// YouTube player after the API code downloads. 
var player; 
function onYouTubePlayerAPIReady() { 
player = new YT.Player('ytplayer', { 
height: '540', 
width: '960', 
videoId: 'UDxzMcCrOyI', 
playerVars: { 'showinfo': 0, 'modestbranding': 1, 'rel': 0, 'iv_load_policy': 3 } 
}); 
} 

当我按下一个按钮,视频被显示,并且还进度条。视频正在自动播放。

$('#ytplayer').show(0, function() { 
player.playVideo(); 

$('#progressBar').show(); 




var playerTotalTime = player.getDuration(); 

mytimer = setInterval(function() { 
var playerCurrentTime = Math.round(player.getCurrentTime()); 

var playerTimeDifference = (playerCurrentTime/playerTotalTime) * 100; 
var playerTimePercent = Math.round(playerTimeDifference); 

console.log(playerTimePercent); 

progress(playerTimePercent, $('#progressBar')); 
}, 1000); 



}); 
+0

你想,或者你自己尝试过的东西吗? – mpgn 2014-08-31 14:57:49

+0

我试过了,我在自己的代码上面做了 – sorinu26 2014-08-31 14:59:14

+0

我在你之前的帖子中回答了你,2天前:http://stackoverflow.com/a/25585727/2274530 – mpgn 2014-08-31 14:59:58

回答

7

尝试这种解决方案:

demo + code

并全面JS代码:

function progress(percent, $element) { 
    var progressBarWidth = percent * $element.width()/100; 

// $element.find('div').animate({ width: progressBarWidth }, 500).html(percent + "%&nbsp;"); 

    $element.find('div').animate({ width: progressBarWidth }); 
} 

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. 
var player; 

function onYouTubeIframeAPIReady() { 
    player = new YT.Player('ytplayer', { 
     height: '540', 
     width: '960', 
     videoId: 'UDxzMcCrOyI', 
     playerVars: { 
     'controls' : 0, 
     'modestbranding' : 1, 
     'rel' : 0, 
     'showinfo' : 0 
     }, 
     events: { 
      'onReady': onPlayerReady, 
      'onStateChange': onPlayerStateChange 
     } 
    }); 
} 

// 4. The API will call this function when the video player is ready. 
function onPlayerReady(event) { 
    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. 

function onPlayerStateChange(event) { 
    if (event.data == YT.PlayerState.PLAYING) { 

     $('#progressBar').show(); 
     var playerTotalTime = player.getDuration(); 

     mytimer = setInterval(function() { 
     var playerCurrentTime = player.getCurrentTime(); 

     var playerTimeDifference = (playerCurrentTime/playerTotalTime) * 100; 


     progress(playerTimeDifference, $('#progressBar')); 
     }, 1000);   
    } else { 

     clearTimeout(mytimer); 
     $('#progressBar').hide(); 
    } 
} 



function stopVideo() { 
    player.stopVideo(); 
} 
+0

工作正常。谢谢! – sorinu26 2014-08-31 15:42:29

+0

你的链接已经死亡? – HuyLe 2017-11-01 15:08:48

+0

链接在这个时刻对我完美的工作 – mpgn 2017-11-02 11:05:34