2017-05-24 2 views
0

私はAVPlayerで曲を再生できるSwift 3.0のプロジェクトに取り組んでいます。私の要求は、曲が再生を終了し、それに応じて再生ボタン画像を更新すると、ポイントを追跡することです。そのためにNSNotificationインスタンスを使用しました。何らかの理由でそれがAVPlayerで曲の再生が終了したときを追跡する方法は?

というエラースロー「キャッチされない例外により 『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

は、あなたのクラスで 'finishedPlaying'メソッドを実装している –

+0

はい私はへのイムは、コード内で「myNotification」を使用して以来、私はそのように行うカントメインコード – danu

答えて

2

変更するには、以下のようにNSNotificationCenterのためにあなたのコードを置き換えることができ

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

func finishedPlaying(_ myNotification:NSNotification) { 

} 
+0

怒鳴る – danu

1

としてメソッドを呼び出すswift3構文に

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

からご通知oberserver

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") 
    } 
+0

uはそれを書いてくださいすることができますコンテキスト午前私 – danu

+0

を超えるコードは、あなたの文脈に完全に答えたいと思っていますか? – KKRocks

関連する問題