iphone - 嵌入的 Youtube 视频在 iPhone 上停止工作

标签 iphone ios youtube

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

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

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

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

加载视频播放器的 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 上工作)?非常感谢任何帮助。

干杯

最佳答案

尝试使用此方法在 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;
}

关于iphone - 嵌入的 Youtube 视频在 iPhone 上停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18179675/

相关文章:

iphone - 将 iPad 应用程序转换为 iPhone 应用程序的最佳实践?

iphone - 添加小数点后的字典上的ios双引号

iphone - 如何跟踪 TableView 单元格中按钮的索引路径?

ios - 无法从 xib 调用 Storyboard

c# - YouTube 数据 API (v3) 在 Windows 服务器 (IIS) 中托管

iphone - iPhone SDK 中的 Tell-A-Friend 形式

ios - Swift 变量约束

iphone - 如何使用委托(delegate)方法在两个 View Controller 之间传递对象?

youtube - API-将PHP中的视频上传到特定 channel

javascript - 如何将Chrome扩展程序连接到Youtube Analytics API?