2012-03-01 80 views
4

这是正确的方式来检查当前播放时间AVAudioPlayer's检查avaudioplayer的当前播放时间

[audioPlayer play]; 

float seconds = audioPlayer.currentTime; 

float seconds = CMTimeGetSeconds(duration); 

我怎样才能代码之后

[audioPlayer play]; 

,当currentTime的是等于11秒,然后

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

和的firstView后当currentTime的是等于23秒

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

Th为你的答案。

+0

安排一个检查时间的并行计时器。 'currentTime'返回一个NSTimeInterval变量,所以'double seconds = audioPlayer.currentTime;'是正确的。 – 0xDE4E15B 2012-03-01 23:09:44

回答

7

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

编辑seconds可能不会正好11或23;你将不得不调整粒度。

+0

我不喜欢1秒的边界围栏。他们似乎不可靠。只要说'如果(秒=> 11){.. performSelector :,然后设置一些标志表明,甚至已经发生。 }' – bobobobo 2013-04-06 22:33:09

相关问题