为什么这个函数中的if语句永远不会成立

时间:2016-02-08 15:55:45

标签: xcode swift if-statement

我在swift UIview中有一个功能,可以制作2 mp4的循环。第一个mp4正常,但第二个没有,它似乎只播放一次,我怎么能解决这个问题?

let videoURL: NSURL = NSBundle.mainBundle().URLForResource(instrumentaimp4[skaicius], withExtension: "mp4")!
        let sakeleURL: NSURL = NSBundle.mainBundle().URLForResource("sakele_blikas", withExtension: "mp4")!

        player = AVPlayer(URL: videoURL)
        player?.actionAtItemEnd = .None
        player?.muted = true

        sakele = AVPlayer(URL: sakeleURL)
        sakele?.actionAtItemEnd = .None
        sakele?.muted = true


        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        playerLayer.zPosition = 1

        let playerLayer2 = AVPlayerLayer(player: sakele)
        playerLayer2.videoGravity = AVLayerVideoGravityResizeAspectFill
        playerLayer2.zPosition = 1



        view.layer.addSublayer(playerLayer2)
        view.layer.addSublayer(playerLayer)

        player?.play()
        sakele?.play()


        //loop video
        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: "loopVideo:",
            name: AVPlayerItemDidPlayToEndTimeNotification,
            object:nil)








func loopVideo(notification: NSNotification) {
    if let finishedPlayer = notification.object as! AVPlayerItem!{

        if finishedPlayer == self.sakele {
            self.sakele?.seekToTime(kCMTimeZero)
            self.sakele?.play()
            NSLog("1")
        }else{
            self.player?.seekToTime(kCMTimeZero)
            self.player?.play()
            NSLog("2")}
    }}

NSLog(" 2")永远不会发生。我的错误在哪里?任何帮助apreciated

1 个答案:

答案 0 :(得分:0)

您的sakele是AVPlayer。但是,您的notification.object声称是(并被投射到)AVPlayerItem,然后将其分配给finishedPlayer。这毫无意义。当你来比较if finishedPlayer == self.sakele时,它永远不会成功,因为它们不是对象的,更不用说同一个对象了。