オーディオファイルの再生終了を知りたい時はAVAudioPlayerのcompletionHandlerを使用する。
しかし、単純に使用すると再生が終わる前にcompletionHandlerがFireされてしまう。
例えばこんな感じ
completionHandler.swift
self.audioFilePlayer.scheduleSegment(self.audioFile,
startingFrame:startPlace,
frameCount: frameCount,
at: time,
completionHandler: playCompletion())
func playCompletion(frameCount:Int, startCount:Int)
{
/* 何か処理*/
}
どのタイミングでcompletionHandlerをFireするかは、completionCallbackTypeを指定することで制御できる。
completionCallbackType.swift
self.audioFilePlayer.scheduleSegment(self.audioFile,
startingFrame:startPlace,
frameCount: frameCount,
at: time,
completionCallbackType: .dataRendered, // <= これ
completionHandler: playCompletion())
func playCompletion(frameCount:Int, startCount:Int)
{
/* 何か処理*/
}
上の例では、.dataRenderedを指定し、再生が終わったらcompletionHandlerがFireされるようにしている。
completionCallbackTypeには他にも、以下のようなものがある。
AVAudioPlayerNodeCompletionCallbackType.swift
public enum AVAudioPlayerNodeCompletionCallbackType : Int {
case dataConsumed
case dataRendered
case dataPlayedBack
}