该视频无法播放,错误代码:152-4

移动开发 2026-07-12

在我的应用中以嵌入方式显示YouTube视频时,它显示错误代码:152 - 4。
我只是想做一个视图,通过传入YouTube ID就能在应用内的特定屏幕/视图中播放该视频。现在在尝试播放时就会出现这个错误。

有没有解决方法?到现在还没找到。

我的代码:

final class YouTubePlayerViewController: UIViewController, WKNavigationDelegate {

    var youtubeId: String
    private var webView: WKWebView!

    init(youtubeId: String) {
        self.youtubeId = youtubeId
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) { fatalError() }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .black
        setupWebView()
        loadPlayer()
    }

    private func setupWebView() {
        let config = WKWebViewConfiguration()
        config.allowsInlineMediaPlayback = true
        config.allowsAirPlayForMediaPlayback = true
        config.allowsPictureInPictureMediaPlayback = true
        config.mediaTypesRequiringUserActionForPlayback = []

        // Required — tells WKWebView it is allowed to autoplay / inline play
        let prefs = WKWebpagePreferences()
        prefs.allowsContentJavaScript = true
        config.defaultWebpagePreferences = prefs

        webView = WKWebView(frame: view.bounds, configuration: config)
        webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        webView.navigationDelegate = self
        webView.scrollView.isScrollEnabled = false
        webView.scrollView.bounces = false
        webView.backgroundColor = .black
        webView.isOpaque = true
        view.addSubview(webView)
    }

    func loadPlayer() {
        // The critical trick: we load a real youtube.com URL as the *base*
        // and inject an autoplay embed via HTML. Because the base URL is
        // genuinely youtube.com, WKWebView passes YouTube's origin check.
        let html = """
        <!DOCTYPE html>
        <html>
          <head>
            <meta name="viewport"
                  content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
            <style>
              html, body {
                margin: 0; padding: 0;
                background: #000;
                width: 100%; height: 100%;
                overflow: hidden;
              }
              iframe {
                position: absolute;
                top: 0; left: 0;
                width: 100%; height: 100%;
                border: none;
              }
            </style>
          </head>
          <body>
            <iframe
              src="https://www.youtube.com/embed/\(youtubeId)?playsinline=1&rel=0&modestbranding=1&autoplay=0&controls=1"
              frameborder="0"
              allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
              allowfullscreen>
            </iframe>
          </body>
        </html>
        """

        // baseURL MUST be a real youtube.com origin — this is what fixes Error 152
        webView.loadHTMLString(html, baseURL: URL(string: "https://www.youtube.com"))
    }

    func updateYoutubeId(_ newId: String) {
        guard newId != youtubeId else { return }
        youtubeId = newId
        loadPlayer()
    }
}

解决方案

视频不可用通常出现在该视频的分享被禁用时。

具体措辞多年来会变化,但我相信在当前版本中你需要启用“允许嵌入”。

你可以在Creator Studio中的该视频页面找到它。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章