有它,等待對不起......
CODEPEN鏈接:http://codepen.io/bra1N/pen/bZjJzQ
HTML:
<div id="player"></div>
<div id="pauseplay">PAUSE</div>
CSS:
#player{
width: 400px;
height: 200px;
}
#pauseplay{
cursor: pointer;
background: cyan;
color: white;
padding: 12px 20px;
width: 100px;
text-align: center;
font-weight: 700;
}
JS:
// 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.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
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.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
//setTimeout(pauseVideo, 6000);
done = true;
}
}
function pauseVideo() {
player.pauseVideo();
}
function playVideo() {
player.playVideo();
}
$('#pauseplay').on('click', function(){
if($(this).hasClass('paused')){
$(this).removeClass('paused');
$(this).text('PAUSE');
playVideo();
} else {
$(this).addClass('paused');
$(this).text('PLAY');
pauseVideo();
}
});
代碼丟失... – ristapk
對不起@ristapk在發佈代碼中有一些錯誤....修正了它!請幫助清除這個問題... –
@ArunCyber所以當你玩你想讓它玩沒有聲音?是? –