试图修复AVAudioPlayer在初次使用时滞后 [英] Trying to fix AVAudioPlayer lag on its initial use

查看:117
本文介绍了试图修复AVAudioPlayer在初次使用时滞后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已在SO上显示在其他几个问题中:

This issue has shown up in several other question here on SO:

AVAudioPlayer第一次播放声音时启动缓慢

使用AVAudioPlayer延迟播放声音

我尝试过实施修正建议,但没有一个能解决我的问题。我的应用程序向用户呈现要触摸的对象网格,当触摸对象时播放声音。这很有效,除了在初始触摸时有大约2秒的延迟。

I've tried implementing the fixes suggested but none of them are solving my problem. My application presents the user with a grid of objects to touch, when an object is touched a sound is played. This works great except on the initial touch there is a delay of ~2 seconds.

为了解决这个问题,我用虚拟的aiff文件初始化我的音频播放器:

To get around this I initialize my audio player with a dummy aiff file:

- (void)viewDidLoad{
    NSString *soundFile = [Card loadCardSoundPath:@"dummy"];

    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:
                             [NSURL fileURLWithPath:soundFile] error:nil];
    self.audioPlayer = player;
    [audioPlayer setDelegate:self];
    [audioPlayer prepareToPlay];
//    [audioPlayer play];
    [player release];
...
[super viewDidLoad]; 
}

然后当触摸一个物体时我打电话:

Then when an object is touched I call:

NSString *soundFile = [Card loadCardSoundPath:name];

if (soundFile != nil){

            AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:
                                     [NSURL fileURLWithPath:soundFile] error:nil];
            self.audioPlayer = player;
            [self.audioPlayer setDelegate:self];
            [self.audioPlayer prepareToPlay];
            [self.audioPlayer play];
            [player release]; 
}

我观察到的行为是这样的:我观察到音频延迟如果我创建一个虚拟音频播放器并调用prepareToPlay但不播放,请单击第一个对象。但是,如果我在虚拟音频播放器上调用播放,那么我不会在初始对象上获得延迟,但视图加载会延迟2秒。

The behavior i'm observing is this: I observe the audio delay when I click the first object if I create a dummy audio player and call prepareToPlay but not play. However if I call play on the dummy audio player then I don't get the delay on the initial object but the view load gets delayed by 2 seconds.

是否有绕过这个的方法?我应该在加载时简单地创建一个AVAudioPlayers的NSArray并告诉他们所有人准备玩,然后在点击对象时调用play?

Is there a way to get around this? Should I simply create an NSArray of AVAudioPlayers on load and tell them all to prepare to play, and then just call play when the object is clicked?

推荐答案

有时它可以帮助让 viewDidLoad 通过使用 peformSelector:withObject:afterDelay:。例如:

Sometimes it can help to let viewDidLoad return by spooling off initialization like that using peformSelector:withObject:afterDelay:. For example:

- (void)viewDidLoad{
    // initialization ...
    [super viewDidLoad]; 
    [self performSelector:@selector(primeAudioPlayer) withObject:nil afterDelay:0.1];
}

-(void)primeAudioPlayer {
    NSString *soundFile = [Card loadCardSoundPath:@"dummy"];

    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:
                             [NSURL fileURLWithPath:soundFile] error:nil];
    [audioPlayer prepareToPlay];
    [player release];
}

这篇关于试图修复AVAudioPlayer在初次使用时滞后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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