我想从屏幕转换目的地控制视频视图的视频播放器。

我想通过轻敲手机来播放视频
试着
//选择(点击)到单元格
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(exist YTPlayerView)和YTPlayerLayer。
我的视图控制器的代码在这里
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()
}
}发布于 2016-09-26 13:25:34
戒烟很简单..你需要像你已经做的那样在didselect中编码。但这类代码在这里永远不会起作用。您必须首先在ViewDidLoad()或ViewWillAppear()中像这样编写代码。
self.player = AVPlayer(url: NSURL(fileURLWithPath: filePath) as URL)
self.playerLayer = AVPlayerLayer(player: self.player)
self.playerLayer.frame = self.vview.bounds然后在didSelect中只编写简单的代码
Player.play()如果您想要更改playerItem,只需在didSelect中编写如下代码。
player.replaceCurrentItemWithPlayerItem(AVPlayerItem(URL: NSURL(fileURLWithPath: filePath)))这是给single View的。试着这样做,如果你需要任何帮助,请随时问我。
https://stackoverflow.com/questions/39671565
复制相似问题