不断将数据从tableview发送到tableview单元类

时间:2014-08-04 23:50:08

标签: ios uitableview avfoundation nstimer avaudioplayer

我有一个NSObject类,其AVAudioPlayerNSTimer我在我的视图控制器中使用,其中包含tableview。当我点击一个单元格时,AVAudioPlayer用于播放与该单元格关联的音频文件,并启动NSTimer。我要做的是发送我的进度变量(浮点数)的值,它等于音频播放器的当前时间除以音频文件的持续时间,返回到我的自定义tableview单元格以更新进度条。

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

1 个答案:

答案 0 :(得分:0)

你可以像这样掌握牢房

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell = [self.table cellForRowAtIndexPath:indexPath];
   /* further code to set up the timer etc...*/
}

一种替代方法是缓存当前选定的单元格并更新单元格上的进度值。

但是,我建议您创建UITableViewCell的子类。在子类中,您有一个播放音频内容并启动计时器的方法。细胞本身现在将观察计时器的进展。请注意,如果您继承UITableViewCell,则可能需要实施prepareForReuse