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

时间:2015-10-16 21:17:49

标签: ios swift video uiwebview swift2

我正在尝试为我的Swift应用程序创建一个视频播放器,但我一直收到'未解析的标识符AVPlayerViewController'的错误。我错过了什么?

我是这方面的初学者,我可能不得不用外行的话来问几千次。我一直在网上搜索可能有一天的视频,如何将Youtube视频嵌入到我的应用程序中,没有结果。如果你能指出一个很棒的教程!

谢谢!

6 个答案:

答案 0 :(得分:33)

Xcode 8.2•Swift 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•Swift 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) )
    }
}

答案 1 :(得分:7)

使用YouTube-Player-iOS-Helper

第1步:将pod 'youtube-ios-player-helper', '0.1.4'添加到 Podfile 并运行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)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

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

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

答案 2 :(得分:3)

Swift 3/4

您可以在XCDYouTubeKit的帮助下在AVPlayer中播放YouTube视频。

添加

  

pod'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)
            }
        }
    }

您可以通过用 medium360 Small240

替换XCDYouTubeVideoQuality。 HD720 .rawValue来更改视频质量

答案 3 :(得分:1)

我通过在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库

答案 4 :(得分:0)

在Swift 4.1中播放youtube视频

添加Pod pod'XCDYouTubeKit'

在ViewController中导入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()
        }
}

答案 5 :(得分:0)

对于iOS12的Swift 4,您应该导入“ 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,我将在下面的示例链接中将其设为批量:https://www.youtube.com/watch?v= gI3pz7eFgfo&t = 838s