Youtube v3 API原点参数不起作用

时间:2016-12-19 00:30:31

标签: ios swift video youtube

我最近在Youtube的v3 API中使用origin参数时遇到了问题。每当我不包含它时,除了播放限制的Vevo类型视频外,我能够播放所有内容。当我尝试播放Vevo视频时,视频只是说播放受到限制。

在线的其他答案表示,将原始参数集添加到https://www.example.com应允许播放Vevo视频。但是,当我这样做并尝试播放Vevo视频时,视频会变黑,甚至没有消息说播放受到限制。此外,当我尝试通过评估Javascript命令播放常规视频时,没有任何反应,并且由于某种原因,我不得不实际点击视频来播放它们。如果有人知道如何通过Youtube的API播放Vevo视频,无论是使用origin参数还是通过其他方法,我都会感激。

以下是播放器的HTML代码:

<!DOCTYPE html>
<html>
    <head>
        <style>
            * { margin: 0; padding: 0; }
            html, body { width: 100%; height: 100%; }
        </style>
    </head>
    <body>
        <div id="player" webkit-playsinline></div>
        <script src="https://www.youtube.com/iframe_api"></script>
        <script>
            var player;
            YT.ready(function() {
                     player = new YT.Player('player', %@);
                     window.location.href = 'ytplayer://onYouTubeIframeAPIReady';
                     });
                     function onReady(event) {
                         window.location.href = 'ytplayer://onReady?data=' + event.data;
                     }
        function onStateChange(event) {
            window.location.href = 'ytplayer://onStateChange?data=' + event.data;
        }
        function onPlaybackQualityChange(event) {
            window.location.href = 'ytplayer://onPlaybackQualityChange?data=' + event.data;
        }
        function onPlayerError(event) {
            window.location.href = 'ytplayer://onError?data=' + event.data;
        }
        </script>
    </body>
</html>

这是我用来初始化播放器并添加它的参数的代码。

fileprivate func loadWebViewWithParameters(_ parameters: YouTubePlayerParameters) {

        // Get HTML from player file in bundle
        let rawHTMLString = htmlStringWithFilePath(playerHTMLPath())!

        // Get JSON serialized parameters string
        let jsonParameters = serializedJSON(parameters as AnyObject)!

        // Replace %@ in rawHTMLString with jsonParameters string
        let htmlString = rawHTMLString.replacingOccurrences(of: "%@", with: jsonParameters)

        // Load HTML in web view
        webView.loadHTMLString(htmlString, baseURL: URL(string: "about:blank"))
    }

    fileprivate func playerHTMLPath() -> String {
        return Bundle(for: self.classForCoder).path(forResource: "YTPlayer", ofType: "html")!
    }

    fileprivate func htmlStringWithFilePath(_ path: String) -> String? {

        do {

            // Get HTML string from path
            let htmlString = try NSString(contentsOfFile: path, encoding: String.Encoding.utf8.rawValue)

            return htmlString as String

        } catch _ {

            // Error fetching HTML
            printLog("Lookup error: no HTML file found for path")

            return nil
        }
    }


    // MARK: Player parameters and defaults

    fileprivate func playerParameters() -> YouTubePlayerParameters {
        playerVars["origin"] = ("https://www.example.com") as AnyObject?
        playerVars["playsinline"] = 1 as AnyObject?
        playerVars["controls"] = 0 as AnyObject?
        playerVars["showinfo"] = 0 as AnyObject?
        return [
            "modestbranding": 1 as AnyObject,
            "height": "100%" as AnyObject,
            "width": "100%" as AnyObject,
            "events": playerCallbacks() as AnyObject,
            "playerVars": playerVars as AnyObject
        ]
    }

    fileprivate func playerCallbacks() -> YouTubePlayerParameters {
        return [
            "onReady": "onReady" as AnyObject,
            "onStateChange": "onStateChange" as AnyObject,
            "onPlaybackQualityChange": "onPlaybackQualityChange" as AnyObject,
            "onError": "onPlayerError" as AnyObject
        ]
    }

我应该尝试除example.com以外的域名吗?这里有另一个问题吗?我获得Youtube播放器代码的Github是https://github.com/gilesvangruisen/Swift-YouTube-Player,可以在问题中找到Swift 3代码。

1 个答案:

答案 0 :(得分:1)

在loadHTMLString中使用https://www.youtube.com/作为baseURL参数:

webView.loadHTMLString(htmlString, baseURL: URL(string: "https://www.youtube.com/"))

然后尝试从你的playerParameters func中删除原始参数 - 这一行:

playerVars["origin"] = ("https://www.example.com") as AnyObject?

它对我来说就像这样。