2

私は Swift と iOS の開発に非常に慣れていないため、私の無知をお許しください。

ビデオの再生が終了したら、AVPlayer を自動的に閉じようとしています。「playerDidFinishPlaying」リスナーをアタッチして通知を受け取ることを考えましたが、一度取得すると、コントローラーを閉じるメソッド/イベントが見つかりません。「完了」ボタンをクリックするアクションを模倣しようとしています。

コードの小さなスニペットを次に示します。うまくいけば、これで十分な情報です。そうでない場合は、さらに情報を提供できます

let destination = segue.destinationViewController as! AVPlayerViewController
let url = NSURL(string: "video url")
destination.player = AVPlayer(URL: url!)
destination.player?.play()

以下の通知を追加しましたが、これもまた、通知が届いたらどうすればよいかわかりません...

NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", 
    name: AVPlayerItemDidPlayToEndTimeNotification, 
    object: destination.player!.currentItem)

func playerDidFinishPlaying(note:NSNotification){
    print("finished")
    // close window/controller
}

最後に、オブザーバーを削除する必要があることはわかっていますが、いつ、どこで削除すればよいかわかりません。どんな助けでも大歓迎です。

4

3 に答える 3

4

コントローラーを「閉じる」には、呼び出す必要がありますdismissViewControllerAnimated(true, completion: nil)

したがって、コードは次のようになります。

NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", 
    name: AVPlayerItemDidPlayToEndTimeNotification, 
    object: destination.player!.currentItem)

func playerDidFinishPlaying(note:NSNotification){
    print("finished")
    dismissViewControllerAnimated(true, completion: nil)
}

viewControllerスタック内にある場合は、次のUINavigationControllerこともできます。

NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", 
        name: AVPlayerItemDidPlayToEndTimeNotification, 
        object: destination.player!.currentItem)

func playerDidFinishPlaying(note:NSNotification){
    print("finished")
    navigationController?.popViewControllerAnimated(true)
}

オブザーバーを削除するには、次の手順で実行できますdeinit{}

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}
于 2016-01-28T16:50:55.500 に答える
0

SWIFT 3 の更新:

注: オーディオのニーズを再生している viewController: AVAudioPlayerDelegate

また、オブザーバーは必要ありません

class myViewController: UIViewController, AVAudioPlayerDelegate {

var audioplayer = AVAudioPlayer() 

override func viewDidAppear(_ animated: Bool) {

if soundsON{
        let myFilePathString = Bundle.main.path(forResource: "backgroundSound", ofType: "mp3")

        if let myFilePathString = myFilePathString
        {
            let myFilePathURL = URL(fileURLWithPath: myFilePathString)

            do{
                try audioplayer = AVAudioPlayer(contentsOf: myFilePathURL)
                audioplayer.delegate = self
                audioplayer.prepareToPlay()

                audioplayer.play()


            }catch{
                print("error playing coin sound")
            }
        }
   }    
}

この例では、サウンドは viewDidAppear で再生され、終了すると次のように呼び出されます: audioPlayerDidFinishPlaying:

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
    //Check if the sound that finishes comes from a certain AVAudioPlayer 
    if player == audioplayer{
        print("The sound from -audioplayer- has finished")
    // If you need to dismiss the VC:
        dismiss(animated: true, completion: nil)
    }
}
于 2016-10-14T16:30:59.870 に答える