使用 WebView 加载 YouTube 不起作用


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

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

  3. 突然间,它停止了工作。它工作的唯一方法是将基本URL设置为像"http://www.example.com"这样的http URL。我想知道要设置的正确基本 URL 是什么。

     <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>
    

    "

我使用"about:blank"。此值不能为 null 或 NONE。

相关内容

  • 没有找到相关文章

最新更新