使用WebView加载YouTube不起作用

时间:2014-01-11 23:16:20

标签: youtube youtube-api

  1. 我曾经在iOS应用中使用HTML字符串播放youtube,如下所示。
  2. 我曾经将HTML加载为

    [webView loadHTMLString:fileContents baseURL:[[NSBundle mainBundle] resourceURL]];

  3. 突然它停止了工作。唯一可行的方法是将baseURL设置为“http://www.example.com”之类的http网址。我想知道要设置的正确baseURL是什么。

     <div id='container'>
        <div id="child">
            <div id="player"></div>
        </div>
    </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', {
                                   width: '%@',
                                   height: '%@',
                                   videoId: '%@',
                                   playerVars: {
                                   origin: 'file://',
                                   showinfo: 0,
                                   controls: 0,
                                   playsinline: 1,
                                   autoplay: 1
                                   },
                                   events: {
                                   'onReady': onPlayerReady,
                                   'onStateChange': onPlayerStateChange,
                                   'onPlaybackQualityChange': onPlayerPlaybackQualityChange,
                                   'onPlaybackRateChange': onPlayerPlaybackRateChange,
                                   'onError': onPlayerError,
                                   'onApiChange': onPlayerApiChange
                                   }
                                   });
        }
    
        // Event functions
        function onPlayerReady(event) {
            onEvent('ready', null);
        }
    
        function onPlayerStateChange(event) {
            onEvent('stateChange', playerStateToString(event.data));
        }
    
        function onPlayerPlaybackQualityChange(event) {
            onEvent('playbackQuality', event.data);
        }
    
        function onPlayerPlaybackRateChange(event) {
            onEvent('playbackRateChange', event.data);
        }
    
        function onPlayerError(event) {
            onEvent('error', event.data);
        }
    
        function onPlayerApiChange(event) {
            onEvent('apiChange', event.data);
        }
    
        function onEvent(eventName, eventData) {
            var url = "ytplayer://event/" + eventName;
            if (eventData != null) {
                url += ('/' + eventData);
            }
            document.location = url;
        }
    
        // Helpers
        function playerStateToString(playerState) {
            var result = null;
            switch (playerState) {
                case -1:
                result = "unstarted";
                break;
                case YT.PlayerState.ENDED:
                result = "ended";
                break;
                case YT.PlayerState.PLAYING:
                result = "playing";
                break;
                case YT.PlayerState.PAUSED:
                result = "paused";
                break;
                case YT.PlayerState.BUFFERING:
                result = "buffering";
                break;
                case YT.PlayerState.CUED:
                result = "cued";
                break;
            }
            return result;
        }
        </script>
    

1 个答案:

答案 0 :(得分:0)

我使用“about:blank”。该值不能为null或NONE。