我想从屏幕转换目标控制视频视图的视频播放器。
我想通过点按小区来播放视频
尝试
//选择(点击)到单元格
override func tableView(_ table: UITableView,didSelectRowAt indexPath: IndexPath) {
let video = ViewController()
video.playerView.playVideo()
}
但是致命的错误:在打开一个可选值时意外地发现了nil
附加:
我尝试使用https://github.com/youtube/youtube-ios-player-helper
中的YTPlayer但我不明白如何写代码" YTPlayer"。
YTPlayer没有YTPlayer(存在YTPlayerView)和YTPlayerLayer。
我的viewcontoller代码
import UIKit
import youtube_ios_player_helper
import AVFoundation
class ViewController: UIViewController, YTPlayerViewDelegate{
@IBOutlet var playerView: YTPlayerView!
var appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
self.playerView.delegate = self
self.playerView.sizeToFit()
let vars = ["playsinline": 1, "controls": 2, "showinfo": 0, "origin":"https://www.google.com"] as [String : Any]
if self.appDelegate.selectMV == ""
{
print("Not Load",self.appDelegate.selectMV)
}
else
{
print("Load",self.appDelegate.selectMV)
playerView.load(withVideoId: self.appDelegate.selectMV, playerVars: vars)
}
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
}
catch {
print("")
}
}
func playerViewDidBecomeReady(_ playerView: YTPlayerView) {
playerView.playVideo()
}
func play() {
print("Push! play")
//Error playerView nil
playerView.load(withVideoId: self.appDelegate.selectMV)
playerView.playVideo()
}
}
答案 0 :(得分:0)
它很简单..你需要在doselect中编写代码。但是这种代码在这里永远不会起作用。你必须在ViewDidLoad()或ViewWillAppear()中这样编码。
self.player = AVPlayer(url: NSURL(fileURLWithPath: filePath) as URL)
self.playerLayer = AVPlayerLayer(player: self.player)
self.playerLayer.frame = self.vview.bounds
然后在didSelect Only代码中只需
Player.play()
如果你想改变playerItem,只需在didSelect中编写代码就像这样..
player.replaceCurrentItemWithPlayerItem(AVPlayerItem(URL: NSURL(fileURLWithPath: filePath)))
这是单一视图。尝试这样,如果您需要任何帮助,请随时问我。