0

我建立了一个包含嵌入式 youtube 视频的网站。直到最近他们还在所有设备上工作,但最近他们已经停止在 iPhone(和 iTouch)上工作。

视频仅播放几秒钟,然后仅显示黑屏。控制台显示常见的 youtube 错误,桌面上的 chrome 和 iphone 上的 safaris 相同:

无法将消息发布到....收件人有来源....

阻止具有来源 .... 的帧访问具有来源 .... 的帧。协议、域和端口必须匹配。

加载视频播放器的 js 改编自 youtube API 示例

// the width of player
var plwidth = 274;
var plheight = 171;

// This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');

// This is a protocol-relative URL as described here:
tag.src = "http://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// Create an <iframe> (and YouTube player) when the buttons are pressed.    
function onYouTubeIframeAPIReady(){
    //alert('youtube downloaded');

    var player;

    var buttons = document.getElementsByClassName('playbutton');

    for(var i = 0; i < buttons.length; i++){        
        buttons[i].style.display = "block";
    }

    // display play buttons
    var button = document.getElementById('playbutton');

    button.onclick = function(){
        player = new YT.Player('player', {
            height: plheight,
            width: plwidth,
            videoId: 'wr7-K42RXh8',
            events: {
                'onReady': onPlayerReady,
            }
        });
        return false;       
    }

}   

// The API will call this function when the video player is ready.
function onPlayerReady(event) {
    var videoplaceholder = document.getElementById('videoplaceholder');
    videoplaceholder.style.display = "none";
    event.target.playVideo(); 
    var button = document.getElementById('playbutton');
    button.style.display = "none";          
}

任何想法为什么它突然停止在 iPhone 上工作(它在 iPad 上工作)?非常感谢任何帮助。

干杯

4

1 回答 1

0

尝试使用此方法在 UIWebView 中播放您的 Youtube 视频。

-(NSString*)htmlStringForEmbeddingYTVideoWithVideoID:(NSString*)videoId andPlayerWindowSide:(CGSize)playerWindowSize
{
    NSString *htmlString = [NSString stringWithFormat:@"<!DOCTYPE html><html><body  style=\"margin:0 0 0 0\" bgcolor=\"#000000\"><!-- 1. The <iframe> (and video player) will replace this <div> tag. --><div id=\"player\" style=\"height:%dpx; width:%dpx;\"></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: '%d',width: '%d',videoId: '%@', playerVars : { 'showinfo' : 0 },events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange}});}   function onPlayerReady(event) { event.target.playVideo();   }   var done = false;   function onPlayerStateChange(event) {   if (event.data == YT.PlayerState.PLAYING && !done) {    setTimeout(stopVideo, 6000);    done = true;    }   }   function stopVideo() {  player.stopVideo(); }   </script>   </body> </html>",playerWindowSize.height,playerWindowSize.width,playerWindowSize.height,playerWindowSize.width,videoId];
    return htmlString;
}
于 2013-08-12T05:23:04.753 回答