【发布时间】:2014-09-27 13:12:27
【问题描述】:
我有一个NSObject 类,它有一个AVAudioPlayer 和一个NSTimer,我在包含我的tableview 的视图控制器中使用它们。当我点击一个单元格时,AVAudioPlayer 用于播放与该单元格关联的音频文件,并启动 NSTimer。我要做的是将我的进度变量(浮点数)的值发送回我的自定义表格视图单元格以更新进度条,该值等于音频播放器的 currentTime 除以音频文件的持续时间。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Recording * recording = [self.fetchCon objectAtIndexPath:indexPath];
NSURL * audioURL = [[NSURL alloc] initWithString:recording.audioURL];
self.audioPlayer = [[OSTablePlayerController alloc] init];
NSData *data=[NSData dataWithContentsOfURL:audioURL];
[self.audioPlayer playAudio:data];
self.audioPlayer.timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.audioPlayer.timer forMode:NSRunLoopCommonModes];
}
- (void)updateProgress:(NSTimer *)timer
{
NSTimeInterval playTime = [self.audioPlayer currentTime];
NSTimeInterval duration = [self.audioPlayer duration];
float progress = playTime/duration;
//send progress variable to tableview cell class
if(!(self.audioPlayer.player.playing))
{
[self audioPlayerDidFinishPlaying:self.audioPlayer.player successfully:YES];
progress = 0.00;
//send progress variable to tableview cell class
}
}
-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if(flag)
{
[self.audioPlayer.timer invalidate];
self.audioPlayer.isPlayerPlaying = NO;
}
}
【问题讨论】:
标签: ios uitableview avfoundation nstimer avaudioplayer