我在swift 3.0中处理一个项目,我可以通过AVPlayer播放歌曲。我的要求是在歌曲播放结束后跟踪该点并相应地更新播放按钮图像。为此,我使用了NSNotification实例。由于某种原因,它会抛出错误说
"由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [Project.ViewController finishedPlaying:]:无法识别的选择器发送到实例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)
}
答案 0 :(得分:3)
从
更改您的通知服从者 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) {
}
答案 1 :(得分:1)
您可以替换NSNotificationCenter的代码,如下所示
update stg.s3_inventory
set last_modified_format = translate(last_modified,'TZ',' ','g')::timestamp
答案 2 :(得分:0)
var streamPlayer = AVPlayer()
func playStreamAudio( for audioUrl:String)
{
guard streamPlayer.timeControlStatus != .playing else {
streamPlayer.pause()
return
}
let url = audioUrl //"http://192.168.71.11:7891/rec.wav"
let playerItem = AVPlayerItem(url: URL(string:url)!)
streamPlayer = AVPlayer(playerItem:playerItem)
streamPlayer.rate = 1.0;
streamPlayer.volume = 1.0
streamPlayer.play()
}