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

How to track when song finished playing in AVPlayer?

我正在 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)

进入 swift 3 语法

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

并将方法调用为

@objc func finishedPlaying( _ myNotification:NSNotification) {

}

您可以如下替换 NSNotificationCenter 的代码

 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)
 NotificationCenter.default.addObserver(self, selector:#selector(self.playerDidFinishPlaying(note:)),name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)


func playerDidFinishPlaying(note: NSNotification) {
        print("Video Finished")
  } 
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()
    }