ios 如何将 Youtube 视频嵌入到我的应用程序中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33179698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to embed a Youtube video into my app?
提问by Dandelyons
I'm trying to create a video player for my Swift app, but I keep getting the error of 'unresolved identifier AVPlayerViewController'. What am I missing?
我正在尝试为我的 Swift 应用程序创建一个视频播放器,但我不断收到“未解析标识符 AVPlayerViewController”的错误消息。我错过了什么?
I'm a beginner at this, I may have to ask a few thousand times in layman's terms. I've been scouring the internet for perhaps a day now for a video for how to embed a Youtube video into my app, with no results. If you could point me over to a tutorial that would be awesome!
我是这方面的初学者,我可能不得不用外行的话问几千次。我已经在互联网上搜索了一天,以获取有关如何将 Youtube 视频嵌入到我的应用程序中的视频,但没有结果。如果你能指点我一个教程,那就太棒了!
Thanks!
谢谢!
回答by Leo Dabus
Xcode 8.2 ? Swift 3.0.2
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 ? Swift 2.x
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) )
}
}
回答by javimuu
Use YouTube-Player-iOS-Helper:
Step 1: add pod 'youtube-ios-player-helper', '0.1.4'
to your Podfileand run pod install
.
第 1 步:添加pod 'youtube-ios-player-helper', '0.1.4'
到您的Podfile并运行pod install
.
Step 2:Implement this code:
第 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)
}
}
Note:Set the rel
key to 0
in your playerVars
dictionary if you don't want to show related video:
注意:如果不想显示相关视频rel
,请0
在playerVars
字典中设置键:
let playerVars = ["playsinline":1, "rel" : 0 ]
let playerVars = ["playsinline":1, "rel" : 0 ]
回答by Chetan
Swift 3/4
斯威夫特 3/4
You can play youtube video in AVPlayer with the help of XCDYouTubeKit.
您可以借助 XCDYouTubeKit 在 AVPlayer 中播放 YouTube 视频。
Add
添加
pod 'XCDYouTubeKit'
吊舱“XCDYouTubeKit”
into your project and write a code as below
进入您的项目并编写如下代码
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)
}
}
}
you can change the quality of video by replacing XCDYouTubeVideoQuality.HD720.rawValue with medium360or with Small240
您可以通过替换 XCDYouTubeVideoQuality 来更改视频质量。HD720.rawValue 与medium360或Small240
回答by Prasanna
I did by using below code on Swift 4+ version
我在 Swift 4+ 版本上使用以下代码
guard let url = URL(string: "<Your YuTube url>")
else{
return
}
let safariViewControllerObject = SFSafariViewController(url: url)
self.present(safariViewControllerObject, animated: true, completion: nil)
Note:- I have imported SafariServices lib
注意:- 我已经导入了 SafariServices 库
回答by Mahesh Chaudhari
Play youtube video in Swift 4.1
在 Swift 4.1 中播放 YouTube 视频
Add Pod pod 'XCDYouTubeKit'
添加 Pod pod 'XCDYouTubeKit'
import XCDYouTubeKit in ViewController
在 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()
}
}
回答by Ivan Obodovskiy
Swift 4 for iOS12 you should import "WebKit" module
Swift 4 for iOS12 你应该导入“WebKit”模块
Make an outlet for WKWebView. Use following function, which you should call in viewDidLoad():
为 WKWebView 做一个出口。使用以下函数,您应该在 viewDidLoad() 中调用它:
func getVideo(videoCode: String) {
let url = URL(string: "https://www.youtube.com/embed/\(videoCode)")
myWKWebView.load(URLRequest(url: url!))
}
For an argument videoCode in function getVideo, you should use the videoID, I will make it bulk on the example link below: https://www.youtube.com/watch?v=gI3pz7eFgfo&t=838s
对于函数 getVideo 中的参数 videoCode,您应该使用 videoID,我将在下面的示例链接上将其批量化:https: //www.youtube.com/watch? v=gI3pz7eFgfo&t=838s