check avaudioplayer's current playback time

故事扮演 提交于 2019-12-03 15:12:18
gregheo

You could set up a NSTimer to check the time periodically. You would need a method like:

- (void) checkPlaybackTime:(NSTimer *)theTimer
{
  float seconds = audioPlayer.currentTime;
  if (seconds => 10.5 && seconds < 11.5) {
    // do something
  } else if (seconds >= 22.5 && seconds < 23.5) {
    // do something else
  }
}

Then set up the NSTimer object to call this method every second (or whatever interval you like):

NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 
  target:self
  selector:@selector(checkPlaybackTime:)
  userInfo:nil
  repeats:YES];

Check the NSTimer documentation for more details. You do need to stop (invalidate) the timer at the right time when you're through with it:

https://developer.apple.com/documentation/foundation/nstimer

EDIT: seconds will probably not be exactly 11 or 23; you'll have to fiddle with the granularity.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!