检查avaudioplayer的当前播放时间 [英] check avaudioplayer's current playback time

查看:368
本文介绍了检查avaudioplayer的当前播放时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

[audioPlayer play];

float seconds = audioPlayer.currentTime;

float seconds = CMTimeGetSeconds(duration); 

我如何编码

  [audioPlayer play]; 

当currentTime等于11秒时

that when currentTime is equal to 11 seconds then

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

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

and after firstview when currentTime is equal to 23 seconds

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

感谢您的回答。

推荐答案

您可以设置 NSTimer 来定期检查时间。你需要一个像这样的方法:

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
  }
}

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

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];

查看NSTimer文档以获取更多详细信息。你需要在正确的时间停止(无效)计时器:

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/library/ios/#documentation/Cocoa/Reference/Foundation/Classes /NSTimer_Class/Reference/NSTimer.html

编辑可能不会完全是11或23;你必须摆弄粒度。

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

这篇关于检查avaudioplayer的当前播放时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆