AVAudioPlayer持续时间更改 [英] AVAudioPlayer Duration Changing

查看:90
本文介绍了AVAudioPlayer持续时间更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用AVAudioPlayer播放音乐文件.它还具有UISlider,用于调整歌曲在播放中的位置.但是,UISlider有一个奇怪的错误,那就是它似乎在改变audioPlayer的持续时间.

My app uses AVAudioPlayer to play music files. It also has a UISlider to adjust where in the song it is playing back. However, there is an odd bug with the UISlider in that it seems to be changing the audioPlayer's duration.

在开始播放歌曲时,作为过程的一部分,将值设置为:

When a song is started, as part of the process, values are set like so:

audioPlayer.currentTime = 0;
seekingSlider.maximumValue = [audioPlayer duration];
seekingSlider.value = audioPlayer.currentTime;

这没问题.然后,我有一个NSTimer,它更新了playedTime和seekSlider.这似乎也没有问题.当用户更改搜索时,将转到以下seekingChanged函数.

This works no problem. I then have an NSTimer that updates the playedTime and the seekingSlider. That also doesn't seem to have a problem. When a user changes the seeking, that then goes to the following seekingChanged function.

- (IBAction)seekingChanged:(id)sender
{
    audioPlayer.currentTime = seekingSlider.value;
    [self updatePlayedTimeLabel:audioPlayer.currentTime];
}


- (void)updatePlayedTimeLabel:(NSTimeInterval)time {
    playedTime.text = [NSString stringWithFormat:@"%d:%02d", (int)time / 60, (int)time % 60, nil];
    [playedTime sizeToFit];
}

请注意,我绝对不会写入audioPlayer.duration.我只是在开始和各种NSLog语句中从中读取内容,以调试发生的情况.令我惊讶的是,它发生了变化.设置值之后,这是一首歌曲开头的示例输出:

Note that at no point do I write to audioPlayer.duration. I only read from it in the beginning and at various NSLog statements to debug what's happening. To my surprise, it changes. Here's one sample output at the beginning of a song, just after setting the values:

after setting, seeking slider is at 0.000000 / 278.981384, audioplayer is at 0.000000 / 278.981375

这是另一个,在暂停了55秒之后:

Here's another one, just after pausing it 55 seconds in:

pausing, seeking slider is at 55.884716 / 278.981384, audioplayer is at 55.946236 / 274.689354

audioPlayer是否存在更改持续时间的已知错误?当用户更改滑块并将其移至下一首歌曲时,由于调用了audioPlayerDidFinishPlaying,因此这本身表现为一个严重的错误.

Is there a known bug with audioPlayer changing the duration? This manifests itself as a bad bug when a user changes the slider and it moves to the next song because audioPlayerDidFinishPlaying is called.

推荐答案

在某些压缩格式下,音频播放器可能会通过播放(并解码)更多和更多地了解歌曲的知识,从而更改对持续时间的估计.更多.这里有类似的观察结果:

Under certain compression formats, the audio player may change its estimate of the duration as it learns more and more about the song by playing (and hence decoding) more and more of it. There's a similar observation here:

音频播放器的持续时间在播放过程中发生了变化

我认为您需要调整滑块所表示的持续时间,以便与音频播放器对持续时间的了解相匹配.

I think you will need to adjust the duration expressed by the slider in order to match the audio player's changes in its knowledge of the duration.

这篇关于AVAudioPlayer持续时间更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆