snukumas February 2016

Why is if statement in this function never true

I have a function in swift UIview that makes 2 mp4`s loop. The first mp4 is playing fine but the second one is not, it only seems to play once, how could i fix this?

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") never happens. Where is my mistake? any help apreciated

Answers


matt February 2016

Your sakele is an AVPlayer. But your notification.object is claimed to be (and is cast to) an AVPlayerItem, which is then assigned to finishedPlayer. This makes no sense. When you come to compare if finishedPlayer == self.sakele, it can never succeed, because they are not the same kind of object, let alone the same object.

Post Status

Asked in February 2016
Viewed 1,756 times
Voted 6
Answered 1 times

Search




Leave an answer