1

我有这段代码可以播放声音,但是第一次播放它需要 5 秒钟才能加载...

我怎样才能加快速度?

-(IBAction)playSound{ //play the cricket noise
 NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Cricket_Sound" ofType:@"mp3"];
 audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
 audioPlayer.delegate = self;
 [audioPlayer prepareToPlay];
 audioPlayer.numberOfLoops = 0;
 [audioPlayer play];
}
4

1 回答 1

0

您可以尝试将以下代码部分放入其中,viewDidLoad以便在要求audioPlayer实例实际播放音频之前准备好播放:

NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Cricket_Sound" ofType:@"mp3"];
 audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
 audioPlayer.delegate = self;
 [audioPlayer prepareToPlay];
 audioPlayer.numberOfLoops = 0;
于 2011-04-29T07:59:02.700 回答