Slow start for AVAudioPlayer the first time a sound is played

后端 未结 9 1336
孤城傲影
孤城傲影 2020-12-02 09:23

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

First, the code:

         


        
9条回答
  •  遥遥无期
    2020-12-02 10:07

    I've taken an alternative approach that works for me. I've seen this technique mentioned elsewhere (tho I don't recall at the moment where that was).

    In short, preflight the sound system by loading and playing a short, "blank" sound before you do anything else. The code looks like this for a short mp3 file I preload in my view controller'sviewDidLoad method that's of .1 second duration:

       NSError* error = nil;
       NSString* soundfilePath = [[NSBundle mainBundle] pathForResource:@"point1sec" ofType:@"mp3"];
       NSURL* soundfileURL = [NSURL fileURLWithPath:soundfilePath];
       AVAudioPlayer* player = [[[AVAudioPlayer alloc] initWithContentsOfURL:soundfileURL error:&error] autorelease];
       [player play];  
    

    You can create your own blank mp3 if you want, or do a google search on "blank mp3s" to find and download one already constructed by somebody else.

提交回复
热议问题