当1结束时,两个视频都会继续重置

时间:2016-02-06 12:47:22

标签: xcode swift video

我在屏幕上播放了2个mp4视频,并且它们都应该循环播放。但当1结束时,它们都会同时重置。我怎么能调整我的代码,以便视频分开循环?

    let videoURL: NSURL = NSBundle.mainBundle().URLForResource("duda", withExtension: "mp4")!
    let sakeleURL: NSURL = NSBundle.mainBundle().URLForResource("sakele_blikas", withExtension: "mp4")!


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

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

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

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



    playerLayer.frame = CGRect(x: 50.0, y: 100.0, width: 240.0, height: 433.0)
    playerLayer2.frame = CGRect(x:647.0, y: 90.0, width: 115.0, height: 44.0)

    view.layer.addSublayer(playerLayer)
    view.layer.addSublayer(playerLayer2)
    player?.play()
    player2?.play()

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

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "loopvideo2", name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)



}

func loopVideo(通知:NSNotification){

if let finishedPlayer = notification.object as! AVPlayer!
{
    if finishedPlayer == self.player2
    {
      self.player2?.seekToTime(kCMTimeZero)
      self.player2?.play()
    } else {
      self.player?.seekToTime(kCMTimeZero)
      self.player?.play()
    }
}

}

继承人错误代码不确定如何格式化

2016-02-08 16:44:15.222 Lietava 2[1928:361895] -[Lietava_2.display loopVideo]: unrecognized selector sent to instance 0x14679cb0

2016-02-08 16:44:15.224 Lietava 2 [1928:361895] *由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [Lietava_2.display loopVideo]:无法识别的选择器发送到实例0x14679cb0' * 第一次抛出调用堆栈: (0x2138810b 0x20b2ee17 0x2138d925 0x2138b559 0x212bbc08 0x2133ce9d 0x2133c8a7 0x2133c685 0x213902db 0x2129ea53 0x2685604b 0x12b6c97 0x12b6c83 0x12bb76d 0x2134b3fd 0x213498f7 0x2129cbf9 0x2129c9e5 0x224e8ac9 0x2552cba1 0x114ea0 0x20f4b873) libc ++ abi.dylib:以NSException类型的未捕获异常终止

1 个答案:

答案 0 :(得分:0)

所以你有这两行代码:

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

NSNotificationCenter.defaultCenter().addObserver(self, 
    selector: "loopvideo2", 
    name: AVPlayerItemDidPlayToEndTimeNotification, 
    object: nil)

实际上,它们正在注册要为完全相同的通知调用的两个函数。您实际上并未确定哪个视频(或视频播放器)刚刚完成。

但你确实有办法做到这一点。

AVPlayerItemDidPlayToEndTimeNotification通知"is the item that finished playing"中传递的对象。

尝试做:

func loopVideo(notification: NSNotification) {

    if let finishedPlayer = notification.object as AVPlayer!
    {
        if finishedPlayer == self.player2
        {
          self.player2?.seekToTime(kCMTimeZero)
          self.player2?.play()
        } else {
          self.player1?.seekToTime(kCMTimeZero)
          self.player1?.play()
        }
    }
}

为此,您需要制作视图控制器的player1和player2属性(而不是只存在于viewDidLoadviewWillAppear函数中的局部变量。