如何将Youtubevideoembedded到我的应用程序中?
我试图为我的Swift应用程序创build一个video播放器,但我不断收到“未parsing标识符AVPlayerViewController”的错误。 我错过了什么?
我是这个初学者,可能要用几十年的时间, 我一直在search互联网,看看如何将Youtubevideoembedded到我的应用中,而没有任何结果。 如果你能指点我的教程,那将是非常棒的!
谢谢!
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") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } 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") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } 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) ) } }
使用YouTube-Player-iOS-Helper :
第1步 :将pod'youtube 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() } }
注意:如果您不想显示相关video,请将您的playerVars
字典中的rel
键设置为0
:
let playerVars = ["playsinline":1, "rel" : 0 ]