YouTube API无法在iOS(iPhone / iPad)中运行,但在桌面浏览器中运行良好?

时间:2013-05-13 01:09:16

标签: javascript ios youtube-api mobile-safari

我在简单的单页网站中使用YouTube API。我能够播放视频,并且我的所有YouTube功能都可以在桌面浏览器中成功运行(IE 7除外 - 也许这个问题会在你帮我回答这个问题时得到解决)但它似乎不起作用完全适用于iOS(iPhone和iPad).YouTube播放器根本无法显示,并且在iOS中根本没有关于YouTube功能的回忆。

以下是我的代码实现。如果我提供的任何问题信息对这个问题有帮助,请告诉我。我想在解决这个问题时尽可能简单地开始,然后在必要时引入其他信息。

/*==========================================================================
    YOUTUBE
========================================================================== */
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 YTready = false,
    playerIdList = [],
    ytPlayers = {};

// When YouTube API is ready, switch YTready to true and run the queue of videos should any exist
// and run a list of page-specified functions such as carousel setups
var YTreadyFunctions = {};
function onYouTubeIframeAPIReady() {
    console.log('YT Ready');
    YTready = true;
    if (playerIdList.length > 0) {
        runYouTubeIframeList(playerIdList);
    }
    for(id in YTreadyFunctions){
        var thisFunc = YTreadyFunctions[id];
        var thisArgs = thisFunc.args;
        thisFunc.func(thisArgs.id,thisArgs.counters,thisArgs.isHome,thisArgs.isAutoRot,thisArgs.galleryType);
    }
}

function shortID(elemId){
    return elemId.split('-').join('');
}

function initializeYouTubeIframe(playerId,params){
        //var playerId = thisList[x];
        var thisPlayer = document.getElementById(playerId);
        ytPlayers[shortID(playerId)] = new YT.Player(playerId, {
            width: thisPlayer.getAttribute('width'),
            height: thisPlayer.getAttribute('height'),
        playerVars: {
            autohide: 1,
            //autoplay: 1,
            theme: 'light',
            showinfo: 0,
            color: 'white',
            rel: 0,
            origin: document.location.hostname,
            wmode: 'transparent'
        },
        videoId: playerId,
            events: {
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
            }
        });
}

function runYouTubeIframeList(thisList) {
    // If the YouTube iFrame API (onYouTubeIframeAPIReady is not ready yet,
    // add the player(s) to a list (playerIdList) to wait until the API is ready and then initialize
    if (!YTready) {
        playerIdList.concat(thisList);
    } else {
        // YT API is ready. Initialize the list of player iframes.
        var thisListLength = thisList.length;
        for (var x = 0; x < thisListLength; x++) {
            initializeYouTubeIframe(thisList[x]);
        }
    }
}
function onPlayerReady(event) {
    //alert('player ready');
}
function onPlayerStateChange(event) {
    //alert('state changed: ' + event.data);
    if (event.data == 3) {
        $('.loading').remove();
    }
    if (event.data == 1) {
        $('#i360-static-panel').css('display','none');
        $('.loading').remove();
        $('div#i360-questions > h2').fadeIn(500).removeClass('transparent');
    }
    if (event.data == 0) {
        $('#i360-answer-mask').slideUp(function () {
            $('p.active-answer').remove();
            $('div#i360-questions > ul').fadeIn(500).removeClass('transparent');
            $('div#i360-questions > ul > li').removeClass('i360-clicked-question');
        });
        $('.i360-shown').fadeOut(300);
        $(event.target.a.parentNode).children('#i360-static-panel').css('display','block');
    }
}

// run through all yt-players and add their ID to a list of to-be-initialized players
$('.yt-player').each(function(i){
    playerIdList.push($(this).attr('id'));
});
runYouTubeIframeList(playerIdList);

function setVideoCarouselThumb(response,element){
    if(response.data){
        if(response.data.thumbnail.hqDefault){
            element.getElementsByTagName('img')[0].src = response.data.thumbnail.hqDefault;
        } else if(response.data.thumbnail.sqDefault){
            element.getElementsByTagName('img')[0].src = response.data.thumbnail.sqDefault;
        }
    } else if (response.status){
        if(response.status == '403'){
            element.setAttribute('class',element.getAttribute('class') ? element.getAttribute('class') + ' private' : 'private');
        }
    }
}
function getYouTubeInfoById(type,getID,callback,args){
    //window.console && console.log('function: getYouTubeInfoById | type = ',type,' | getId = ',getID,' | args = ',args);
    //consoleThis('https://gdata.youtube.com/feeds/api/' + type + '/' + getID + '?v=2&prettyprint=true&alt=jsonc');
    $.ajax({
        url: 'https://gdata.youtube.com/feeds/api/' + type + '/' + getID + '?v=2&prettyprint=true&alt=jsonc',
        dataType: 'jsonp',
        context: args ? args : '',
        success: function(response){
            callback(response,this);
        },
        error: function(response){
            callback(response,this);
            //consoleThis(response);
        }
    });
}

// END YOUTUBE
</script>


    <meta name="apple-mobile-web-app-title" content="Roundup" />
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-title" content="Roundup" />
    <meta name="viewport" content="minimum-scale=1.0, maximum-scale=1.0">

1 个答案:

答案 0 :(得分:11)

根据this other SO post,iOS上存在限制,其中“...嵌入式媒体无法在iOS上的Safari中以编程方式自动 - 用户始终启动播放。”

我遇到了同样的问题,结果发现你可以正确嵌入了YouTube-API IFrame并在iOS上显示了YouTube的大红色“播放”按钮,这只是iOS不会让您可以使用自己的控件启动/停止视频。 JavaScript的。例如。您可以使用JavaScript进行下一个/上一个,但无法播放/暂停,必须通过单击上的视频,然后使用视频进度条旁边的原生控件来完成。

但是关于你的第一段的最后一句并且放弃这些限制,YouTube播放器应该显示,你所做的事情有问题(尽管我不够专业,无法说出什么)。我也在a simple one-page website中这样做,随便看看我做了什么。

希望有所帮助!