我一直在進行故障診斷並搜索了幾個小時的答案,但無濟於事。 API文檔中的代碼示例正在逐字使用。其他人發佈和說在IE9上工作的示例代碼不適合我。我已經確認了doctype在html中正確設置,我沒有在CSS中使用display = none隱藏iframe,瀏覽器模式是IE9,文檔模式是IE9標準。我的安全設置配置爲中等偏高。即使在我的IE9瀏覽器中,谷歌的示例頁面也不適用於我。YouTube iframe API - onReady和onStateChanged事件在IE9中未觸發
https://developers.google.com/youtube/youtube_player_demo
當我點擊「播放」,力圖使影片播放(在上面的鏈接),我得到的各種錯誤的腳本調試IE控制檯。第一個錯誤是這樣的:
SCRIPT5009: '視頻' 是不確定的
請幫幫忙!我的示例代碼如下。電影加載後,我看到「API已準備就緒」警報,但其他事件都不會在IE9中觸發。我只是不知道我錯過了什麼!如果我不能很快了解這一點,我將不得不放棄在我的項目中使用YouTube進行html5視頻播放。
<!DOCTYPE html>
<html>
<head><title></title></head>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>
<script type="text/javascript">
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
// This is a protocol-relative URL as described here:
// http://paulirish.com/2010/the-protocol-relative-url/
// If you're testing a local page accessed via a file:/// URL, please set tag.src to
// "https://www.youtube.com/iframe_api" instead.
//tag.src = "//www.youtube.com/iframe_api";
tag.src = "https://www.youtube.com/iframe_api";
//tag.src = "http://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() {
alert("API is ready!");
player = new YT.Player('player', {
height: '450',
width: '800',
videoId: 'mU7aJgvh9K8',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
alert("Player is ready!");
}
// 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) {
alert("State changed!");
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>
我沒有足夠的聲望投票或評論,但我想說謝謝,因爲這解決了我一整天的頭痛!如果我找到讓他們一起工作的方法,我會回覆。編輯:如果它幫助別人;我們通過使用定位來顯示和隱藏YouTube容器而不是使用「display:none」或「visibility:hidden」來解決此問題。 – Wayferer 2013-04-10 00:03:46
關於甚至不使用'visibility:hidden'來代替'display:none'的部分是我的優點。謝謝。 – 2014-03-05 14:39:24