swift2 如何将Youtube视频嵌入我的应用程序?

i34xakig  于 2022-11-06  发布在  Swift
关注(0)|答案(7)|浏览(227)

我正在尝试为我的Swift应用创建视频播放器,但我一直收到“未解析标识符AVPlayerViewController”的错误。我错过了什么?
我是一个初学者,我可能要问几千次外行的术语。我已经在互联网上搜索了大概一天了,为了找到一个视频,如何嵌入一个Youtube视频到我的应用程序,没有结果。如果你能给我一个教程,这将是真棒!
谢谢你!

gv8xihay

gv8xihay1#

Xcode 8.2 ·斯威夫特3.0.2版

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var wv: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        loadYoutube(videoID: "oCm_lnoVf08")
    }
    func loadYoutube(videoID:String) {
        guard
            let youtubeURL = URL(string: "https://www.youtube.com/embed/\(videoID)")
            else { return }
        wv.loadRequest( URLRequest(url: youtubeURL) )
    }
}

Xcode 7.3.1 ·斯威夫特2.x

import UIKit

class ViewController: UIViewController {
    // create an outlet for your webview 
    @IBOutlet weak var wv: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // load your you tube video ID
        loadYoutube(videoID: "oCm_lnoVf08")
    }
    func loadYoutube(videoID videoID:String) {
        // create a custom youtubeURL with the video ID
        guard
            let youtubeURL = NSURL(string: "https://www.youtube.com/embed/\(videoID)")
            else { return }
        // load your web request
        wv.loadRequest( NSURLRequest(URL: youtubeURL) )
    }
}
2wnc66cl

2wnc66cl2#

斯威夫特3/4

您可以在XCDYouTubeKit的帮助下在AVPlayer中播放youtube视频。
新增
转接盒'XCDYouTubeKit'
并编写如下代码

func playVideo() {

        let playerViewController = AVPlayerViewController()
        self.present(playerViewController, animated: true, completion: nil)

        XCDYouTubeClient.default().getVideoWithIdentifier("KHIJmehK5OA") { (video: XCDYouTubeVideo?, error: Error?) in
            if let streamURL = video?.streamURLs[XCDYouTubeVideoQuality.HD720.rawValue] {
                playerViewController.player = AVPlayer(url: streamURL)
            } else {
                self.dismiss(animated: true, completion: nil)
            }
        }
    }

您可以通过将XCDYouTubeVideoQuality.HD 720.rawValue替换为中360小240来更改视频质量

ht4b089n

ht4b089n3#

使用YouTube-Player-iOS-Helper

第1步:将pod "youtube-ios-player-helper", "~> x.y.z"添加到您的***播客文件***中,用最新版本替换“x.y.z”,然后运行pod install
**步骤2:**实作此程式码:

import UIKit
import youtube_ios_player_helper

class ViewController: UIViewController, YTPlayerViewDelegate {
    @IBOutlet weak var playerView: YTPlayerView!

    override func viewDidLoad() {
        super.viewDidLoad()
        playerView.delegate = self

        let playerVars = ["playsinline": 1] // 0: will play video in fullscreen
        self.playerView.loadWithVideoId("youtubeId", playerVars: playerVars)
    }     
}

***注意:***如果您不想显示相关视频,请在playerVars字典中将rel键设置为0

let playerVars = ["playsinline":1, "rel" : 0 ]

klh5stk1

klh5stk14#

我在Swift 4+版本上使用了以下代码

guard let url = URL(string: "<Your YuTube url>")
        else{
            return
        }

let safariViewControllerObject = SFSafariViewController(url: url)
self.present(safariViewControllerObject, animated: true, completion: nil)

注意:-我已导入SafariServices库

35g0bw71

35g0bw715#

在Swift 4.1中播放YouTube视频

添加Pod Pod 'XCDYouTubeKit'
在视图控制器中导入XCDYouTubeKit

func playYoutubeVideo(){

    let strUrl = "https://www.youtube.com/watch?v=9m_K2Yg7wGQ"
    if let range = strUrl.range(of: "=") {
            let strIdentifier = strUrl.substring(from: range.upperBound)
            print("Identifier:\(strIdentifier)")
            let videoPlayerViewController = 
            XCDYouTubeVideoPlayerViewController(videoIdentifier: strIdentifier)
            videoPlayerViewController.present(in: viewYTVideo)
            videoPlayerViewController.moviePlayer.play()
        }
}
6ju8rftf

6ju8rftf6#

Swift 4 for iOS12您应该导入“WebKit”模块
为WKWebView创建一个出口,使用以下函数,该函数应在viewDidLoad()中调用:

func getVideo(videoCode: String) {
    let url = URL(string: "https://www.youtube.com/embed/\(videoCode)")
    myWKWebView.load(URLRequest(url: url!))
}

对于函数getVideo中的参数videoCode,您应该使用videoID,我将在下面的示例链接中批量使用它:************

6za6bjd0

6za6bjd07#

import WebKit
import SnapKit

class YoutubeVideoView: UIView {

    private lazy var webConfig: WKWebViewConfiguration = {
        $0.allowsInlineMediaPlayback = true
        return $0
    }(WKWebViewConfiguration())

    private lazy var webView: WKWebView = {
        $0.scrollView.isScrollEnabled = false
        return $0
    }(WKWebView(frame: .zero, configuration: webConfig))

    private var url: String

    init(url: String) {
        self.url = url
        super.init(frame: .zero)
        configureViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func configureViews() {
        addSubview(webView)
        webView.snp.makeConstraints({ $0.edges.equalToSuperview() })
    }

    func play() {
        guard let url = URL(string: url) else { return }
        webView.load(.init(url: url))
    }
}

相关问题