我在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
答案 0 :(得分:0)
您的sakele
是AVPlayer。但是,您的notification.object
声称是(并被投射到)AVPlayerItem,然后将其分配给finishedPlayer
。这毫无意义。当你来比较if finishedPlayer == self.sakele
时,它永远不会成功,因为它们不是对象的种,更不用说同一个对象了。