嵌入式Youtube视频停止在iPhone上运行

时间:2013-08-12 04:46:24

标签: iphone ios youtube

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

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

无法发送消息给....收件人有来源....

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

从youtube API示例中添加了加载视频播放器的js

// 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)?任何帮助都非常感谢。

欢呼声

1 个答案:

答案 0 :(得分: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;
}