【问题标题】:check avaudioplayer's current playback time查看avaudioplayer的当前播放时间
【发布时间】:2012-03-01 22:41:07
【问题描述】:

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

[audioPlayer play];

float seconds = audioPlayer.currentTime;

float seconds = CMTimeGetSeconds(duration); 

之后我如何编码

  [audioPlayer play]; 

当 currentTime 等于 11 秒时

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

当 currentTime 等于 23 秒时在 firstview 之后

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

【问题讨论】:

  • 安排一个检查时间的并行计时器。 currentTime 返回一个 NSTimeInterval 变量,所以 double seconds = audioPlayer.currentTime; 是正确的。

标签: ios avaudioplayer


【解决方案1】:

您可以设置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
  }
}

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

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

查看 NSTimer 文档了解更多详情。您确实需要在正确的时间停止(无效)计时器:

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

编辑seconds 可能不会正好是 11 或 23;您将不得不摆弄粒度。

【讨论】:

  • 我不喜欢 1 秒的边界围栏。他们似乎不可靠。就说if (seconds =&gt; 11) { .. performSelector:, then set some flag indicating that even already happened. }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-12
  • 1970-01-01
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
相关资源
最近更新 更多