gpt4 book ai didi

ios - 如何跟踪歌曲在 AVPlayer 中播放完毕的时间?

转载 作者:搜寻专家 更新时间:2023-11-01 06:16:20 25 4
gpt4 key购买 nike

我正在使用 swift 3.0 开发一个项目,我可以通过 AVPlayer 播放歌曲。我的要求是在歌曲播放完毕后跟踪该点并相应地更新播放按钮图像。为此,我使用了一个 NSNotification 实例。出于某种原因,它抛出一个错误

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Project.ViewController finishedPlaying:]: unrecognized selector sent to instance 0x7fda8e928170'".

我在这里错过了什么??我的代码如下。

 @IBAction func playButtonPressed(_ sender: Any) {

if newRowSelected == true{
if(isNewPathSelected){
print("player")
let url:URL = savePath!
playerItem = AVPlayerItem(url: url as URL)
player=AVPlayer(playerItem: playerItem!)
let playerLayer=AVPlayerLayer(player: player!)
playerLayer.frame = CGRect(x: 0, y: 0, width: 10, height: 50) // actually this player layer is not visible
self.view.layer.addSublayer(playerLayer)
isNewPathSelected = false
}

if player?.rate == 0 // this means if its not playing
{
print("Playing")
player!.play()
playButton.setImage(UIImage(named: "pause_button"), for: UIControlState.normal)

trackTime()

if playerItem != nil {
print("playerItem")
NotificationCenter.default.addObserver(self, selector: Selector(("finishedPlaying:")), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)

}

} else {
player!.pause()
playButton.setImage(UIImage(named: "play_button"), for: UIControlState.normal)
}
}else{
showAlert()
}

}

func finishedPlaying(myNotification:NSNotification) {
playButton.setImage(UIImage(named: "play_button"), for: UIControlState.normal)

let stopedPlayerItem: AVPlayerItem = myNotification.object as! AVPlayerItem
stopedPlayerItem.seek(to: kCMTimeZero)
}

最佳答案

改变你的通知观察者

 NotificationCenter.default.addObserver(self, selector: Selector(("finishedPlaying:")), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)

进入swift3语法

  NotificationCenter.default.addObserver(self, selector: #selector(self.finishedPlaying(_:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)

并将方法调用为

@objc func finishedPlaying( _ myNotification:NSNotification) {

}

关于ios - 如何跟踪歌曲在 AVPlayer 中播放完毕的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44152949/

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