2011-11-28 133 views
3

我想在播放音频时在音频标签上显示播放时间/总持续时间。我已经看过AVAudioPlayerDelegate方法,但是我找不到这样的方法,它会在播放后每秒钟通知它。我不喜欢这样写道:如何使用AVAudioPlayer更新播放音频的播放时间使用AVAudioPlayer

-(IBAction)playAudio{ 

NSBundle *bundle = [NSBundle mainBundle]; 
NSString *audioPath = [bundle pathForResource:@"PhirMohobat" ofType:@"mp3"]; 
NSURL *audioUrl = [NSURL fileURLWithPath:audioPath]; 


player = [[AVAudioPlayer alloc]initWithContentsOfURL:audioUrl error:NULL]; 
player.delegate = self; 

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; 
[player play]; 

} 

-(void)updateTime 
{ 

float minutes = floor(player.currentTime/60); 
float seconds = player.currentTime - (minutes * 60); 

float duration_minutes = floor(player.duration/60); 
float duration_seconds = player.duration - (duration_minutes * 60); 

NSString *timeInfoString = [[NSString alloc] 
          initWithFormat:@"%0.0f.%0.0f/%0.0f.%0.0f", 
          minutes, seconds, 
          duration_minutes, duration_seconds]; 


timerLabel.text = timeInfoString; 
[timeInfoString release]; 

}

有任何隐含的委托方法在AVAudioPlayer或Xcode中支持的任何其他音频播放器做到这一点?

回答

2

AVAudioPlayer具有一个名为currentTime的属性,因此您可以使用NSTimer在一定的时间间隔内询问该属性的实际值,然后根据此值更新任何您需要的值。

+0

谢谢你的回答,但我已经尝试了它,并对新方法感兴趣,我想要一个显式回调或任何其他库提供它,而不使用定时器 – indu

相关问题