gpt4 book ai didi

xcode - 1 结束后两个视频都会重置

转载 作者:行者123 更新时间:2023-11-30 13:43:42 24 4
gpt4 key购买 nike

我的屏幕上正在播放 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)



}

funcloopVideo(通知: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 0x12b 6c97 0x12b6c83 0x12bb76d 0x2134b3fd 0x213498f7 0x2129cbf9 0x2129c9e5 0x224e8ac9 0x2552cba1 0x114ea0 0x20f4b873)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()
}
}
}

为此,您需要为 View Controller 设置player1和player2属性(而不是仅存在于viewDidLoadviewWillAppear函数中的局部变量)。

关于xcode - 1 结束后两个视频都会重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35241184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com