首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从不同的视图引用YTPlayer将变为nil

从不同的视图引用YTPlayer将变为nil
EN

Stack Overflow用户
提问于 2016-09-24 09:13:14
回答 1查看 396关注 0票数 0

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

我想通过轻敲手机来播放视频

试着

//选择(点击)到单元格

代码语言:javascript
复制
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。

我的视图控制器的代码在这里

代码语言:javascript
复制
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()
    }

}
EN

回答 1

Stack Overflow用户

发布于 2016-09-26 13:25:34

戒烟很简单..你需要像你已经做的那样在didselect中编码。但这类代码在这里永远不会起作用。您必须首先在ViewDidLoad()或ViewWillAppear()中像这样编写代码。

代码语言:javascript
复制
self.player = AVPlayer(url: NSURL(fileURLWithPath: filePath) as URL)
self.playerLayer = AVPlayerLayer(player: self.player)
self.playerLayer.frame = self.vview.bounds

然后在didSelect中只编写简单的代码

代码语言:javascript
复制
Player.play()

如果您想要更改playerItem,只需在didSelect中编写如下代码。

代码语言:javascript
复制
player.replaceCurrentItemWithPlayerItem(AVPlayerItem(URL: NSURL(fileURLWithPath: filePath)))

这是给single View的。试着这样做,如果你需要任何帮助,请随时问我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39671565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档