我正在参加一门在线类(class),该类(class)展示了如何从 url 播放视频。但它始终是 bunny one .我一直在寻找如何播放 YouTube 视频,发现他们使用 UIWebView。既然它已被弃用,我现在怎么能玩它们。这就是我的代码的样子。
import UIKit
import AVKit
import AVFoundation
class CourseDetailVC: UIViewController {
@IBOutlet weak var descriptionTextView: UITextView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var authorButton: UIButton!
@IBOutlet weak var backgroundImage: UIImageView!
var course: Courses.Course?
@IBAction func playURLVideo() {
guard let videoURL = URL(string: course?.videoUrl ?? "") else { return }
let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
我只有一个按钮。当我按下它时,我想播放 YouTube 视频。现在这适用于兔子。我怎么能玩这个 YouTube video .
@IBAction func playButtonPress() {
// Create Video player
var mywkwebview: WKWebView?
let mywkwebviewConfig = WKWebViewConfiguration()
mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)
let myURL = URL(string: "https://www.youtube.com/embed/JePnQ1gSagc?playsinline=1?autoplay=1")
let youtubeRequest = URLRequest(url: myURL!)
mywkwebview?.load(youtubeRequest)
guard let webView = mywkwebview else { return }
self.view.addSubview(webView)
}
这里是WKWebView
由于 YouTube 具有使用格式为 https://www.youtube.com/embed/
的 URL 以全屏方式在 web View 中加载任何视频的功能,而无需任何滚动功能.
以上给定视频的 ID 为 (https://www.youtube.com/watch?v=1roy4o4tqQM)
-- [1roy4o4tqQM]
mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)
let myURL = URL(string: "https://www.youtube.com/embed/1roy4o4tqQM?playsinline=1?autoplay=1")
var youtubeRequest = URLRequest(url: myURL!)
mywkwebview.load(youtubeRequest)
@IBAction func playURLVideo() {
var mywkwebview: WKWebView?
let mywkwebviewConfig = WKWebViewConfiguration()
mywkwebviewConfig.allowsInlineMediaPlayback = true
mywkwebview = WKWebView(frame: self.view.frame, configuration: mywkwebviewConfig)
let myURL = URL(string: "https://www.youtube.com/embed/1roy4o4tqQM?playsinline=1?autoplay=1")
var youtubeRequest = URLRequest(url: myURL!)
mywkwebview.load(youtubeRequest)
}
你需要在嵌入URL
?autoplay=1
关于ios - 我们现在如何使用 iOS 播放 YouTube 视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53313306/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |