if函数语句

时间:2017-05-15 14:04:13

标签: ios swift

如果某个视图被点击一次,我正在尝试制作视频。如果我直接在函数内部的if语句中调用.play()函数(if“检查”setupPlayerView()函数内的URL),setupPlayerView函数就可以正常工作。我将在下面显示的第一个函数(defInteractionsplayer)将在覆盖初始化中调用,该初始化设置视频播放器的属性和子视图/子图层等。最后一个函数由第二个函数触发。请注意func setupPlayerView() { //insert url let urlString = "https://blurtime.com/images/testvideo.mov" //check URL if can be converted to NSURL if let videoURL = NSURL(string: urlString){ //player's video let player = AVPlayer(url: videoURL as URL) //add sub-layer let playerLayer = AVPlayerLayer(player: player) self.layer.addSublayer(playerLayer) playerLayer.frame = self.frame //when are frames actually rendered (when is video loaded) player.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil) //if I call player.play() here the video plays directly } } 的声明以及我在底部的评论......

代码:func#1

//set interactions
func defInteractions (){
    //enable interaction
    controlsContainerView.isUserInteractionEnabled = true

    //singletap
    let singleTap = UITapGestureRecognizer(target: self, action: #selector(singleTapDetected(_:)))
    singleTap.numberOfTapsRequired = 1

    //controlsContainerView
    controlsContainerView.addGestureRecognizer(singleTap)
}

当我检测单击的UITapGestureRecognizers时,我有以下函数,它也会在覆盖init中调用(在上一个函数之后不久):

代码:func#2

player.play()

现在,我想在函数singleTapDetected中调用func singleTapDetected(_ sender: UITapGestureRecognizer) { player.play() } ,目前看起来像这样:

代码:func#3

use of unresolved identifier 'player'

然而,它当然不起作用,因为这个函数在覆盖init之外而不是其他函数,我得到错误player.play()。如何调用mvn clean install -DskipTests并获得相同的结果,就像我在第一个函数中调用它一样?我可以在if中访问它吗?我需要一些帮助...

4 个答案:

答案 0 :(得分:1)

使.f-item { width: calc(20% - 16px); margin-right: 20px; background-color: green; text-align: center; color: white; } .f-item:last-child { margin-right: 0; } 成为实例变量:

player

然后像这样初始化它:

var player: AVPlayer?

然后您可以从班级的任何地方轻松访问它。

答案 1 :(得分:1)

播放器属性设为全局,如下所示:

let player : AVPlayer?

setupPlayerView()

中初始化它
func setupPlayerView() {

//insert url
let urlString = "https://blurtime.com/images/testvideo.mov"

//check URL if can be converted to NSURL
if let videoURL = NSURL(string: urlString){

    //player's video
    self.player = AVPlayer(url: videoURL as URL)

    //add sub-layer
    let playerLayer = AVPlayerLayer(player: player)
    self.layer.addSublayer(playerLayer)
    playerLayer.frame = self.frame

    //when are frames actually rendered (when is video loaded)
    player.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)

    //if I call player.play() here the video plays directly

}

}

并像这样使用它:

func singleTapDetected(_ sender: UITapGestureRecognizer) {
    // with self
    self.player.play()

} 

希望有所帮助

答案 2 :(得分:1)

使用可选项在类级别声明try/catch

player

答案 3 :(得分:0)

不应将player声明为setupPlayerView函数中的局部变量,而应将其声明为可在整个类/结构范围内访问的实例变量。

对于您的情况,我建议不要实现setupPlayerView函数,将它声明为 lazy 属性是一个好习惯,如下所示:

lazy var player: AVPlayer? = {
    //insert url
    let urlString = "https://blurtime.com/images/testvideo.mov"

    guard let videoURL = NSURL(string: urlString) else {
        return nil
    }

    let player = AVPlayer(url: videoURL as URL)

    //add sub-layer
    let playerLayer = AVPlayerLayer(player: player)
    self.layer.addSublayer(playerLayer)
    playerLayer.frame = self.frame

    //when are frames actually rendered (when is video loaded)
    player.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)

    return player
}()

用法:

// this means if the urlString is valid
if let player = player {
    player.play()
}

这会导致:如果需要,player所需的所有设置都应该 。您可能需要查看此Q&A