使用UISlider跟踪声音文件的播放时间

时间:2011-10-17 22:29:26

标签: iphone objective-c ios

我播放声音文件:

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/recordTest.caf", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];

sliderUpdater = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                 target:self
                                               selector:@selector(sliderUpdateTimer)
                                               userInfo:nil
                                                repeats:YES];

声音播放,我启动NSTimer调用我的方法sliderUpdateTimer。

所以方法是

- (void) sliderUpdateTimer
{                
NSLog(@"Dur: %f",audioPlayer.duration );
NSLog(@"Cur: %f",audioPlayer.currentTime );    
[seekSlider setValue:100 * (audioPlayer.duration) / (audioPlayer.currentTime)];
}

持续时间和当前时间都显示为0.

这是我做错了什么部分?

audioPlayer.duration和currentTime返回一个NSTimerInterval,我相信它是双倍的。

非常感谢, -code

1 个答案:

答案 0 :(得分:0)

这些组合对我来说很好,

-(IBAction)playAction
{
    paused = NO;
    [audioPlayer play];
    audioSlider.maximumValue = [audioPlayer duration];
    audioSlider.value = 0.0;

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

- (void)updateTime:(NSTimer *)timer
{
    audioSlider.value = audioPlayer.currentTime;
    audioTimer.text = [NSString stringWithFormat:@"%d", audioSlider.value];
}