我在屏幕上播放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(notification: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 0x2b2f9e002
libc ++ abi.dylib:以类型为NSException的未捕获异常终止

最佳答案

因此,您有这两行代码:

//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函数中的局部变量)。

07-28 03:04
查看更多