检查avaudioplayer的当前播放时间

use*_*133 5 avaudioplayer ios

这是检查AVAudioPlayer's当前播放时间的正确方法.

[audioPlayer play];

float seconds = audioPlayer.currentTime;

float seconds = CMTimeGetSeconds(duration); 
Run Code Online (Sandbox Code Playgroud)

我怎么可以编码

  [audioPlayer play]; 
Run Code Online (Sandbox Code Playgroud)

当currentTime等于11秒时

[self performSelector:@selector(firstview) withObject:nil]; 
Run Code Online (Sandbox Code Playgroud)

并且在firstTime等于23秒后的第一次查看

[self performSelector:@selector(secondview) withObject:nil];
Run Code Online (Sandbox Code Playgroud)

谢谢你的回答.

gre*_*heo 8

您可以设置a NSTimer定期检查时间.您需要一个方法,如:

- (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
  }
}
Run Code Online (Sandbox Code Playgroud)

然后设置NSTimer对象每秒调用此方法(或您喜欢的任何间隔):

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

有关更多详细信息,请查看NSTimer文档.您确实需要在正确的时间停止(无效)计时器:

https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html

编辑:seconds可能不会是11或23; 你必须摆弄粒度.