慢启动的AVAudioPlayer第一次发出声音 [英] Slow start for AVAudioPlayer the first time a sound is played

查看:124
本文介绍了慢启动的AVAudioPlayer第一次发出声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图消除启动延迟播放时(很短 - 不到2秒)通过AVAudioPlayer iPhone上的音频文件。

I'm trying to eliminate startup lag when playing a (very short -- less than 2 seconds) audio file via AVAudioPlayer on the iPhone.

首先,code:

NSString *audioFile = [NSString stringWithFormat:@"%@/%@.caf", [[NSBundle mainBundle] resourcePath], @"audiofile"];
NSData *audioData = [NSData dataWithContentsOfMappedFile:audioFile];

NSError *err;
AVAudioPlayer *audioPlayer = [(AVAudioPlayer*)[AVAudioPlayer alloc] initWithData:audioData error:&err];

audioPlayer.delegate = self;
[audioPlayer play];

我也实现了audioPlayerDidFinishPlaying方法来释放AVAudioPlayer,一旦我完成了。

I also implement the audioPlayerDidFinishPlaying method to release the AVAudioPlayer once I'm done.

我第一次播放音频的滞后性是显而易见的 - 至少2秒。然而,在此之后,声音立即播放。我怀疑是罪魁祸首,那么,是[NSData的dataWithContentsOfMappedFile]花费很长的时间从闪存读取开始,但随后很快被后来上读取。我不知道如何测试,虽然。

The first time I play the audio the lag is palpable -- at least 2 seconds. However, after that the sound plays immediately. I suspect that the culprit, then, is the [NSData dataWithContentsOfMappedFile] taking a long time reading from the flash initially, but then being fast on later reads. I'm not sure how to test that, though.

是这样吗?如果是这样,我应该只是pre-缓存NSData的对象和侵略性有关在内存不足的情况​​清除它们?

Is that the case? If so, should I just pre-cache the NSData objects and be aggressive about clearing them in low memory conditions?

推荐答案

延迟似乎与实例AVAudioPlayer首次。如果我加载任何音频,运行[audioPlayer prepareToPlay],然后马上松开,我所有音频的加载时间非常接近,不易察觉。所以,现在我在做在的applicationDidFinishLaunching和其他一切运行良好。

The delay seems to be related to instantiating AVAudioPlayer for the first time. If I load any audio, run [audioPlayer prepareToPlay] and then immediately release it, the load times for all of my other audio is very close to imperceptible. So now I'm doing that in applicationDidFinishLaunching and everything else runs well.

我找不到这事的文档,但它肯定似乎是这样的。

I can't find anything about this in the docs, but it certainly seems to be the case.

这篇关于慢启动的AVAudioPlayer第一次发出声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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