check avaudioplayer's current playback time

淺唱寂寞╮ 提交于 2019-12-21 05:11:27

问题


Is this the right way to check AVAudioPlayer's current playback time.

[audioPlayer play];

float seconds = audioPlayer.currentTime;

float seconds = CMTimeGetSeconds(duration); 

How I can code after

  [audioPlayer play]; 

that when currentTime is equal to 11 seconds then

[self performSelector:@selector(firstview) withObject:nil]; 

and after firstview when currentTime is equal to 23 seconds

[self performSelector:@selector(secondview) withObject:nil];

回答1:


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.



来源:https://stackoverflow.com/questions/9525357/check-avaudioplayers-current-playback-time

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