Slow start for AVAudioPlayer the first time a sound is played

后端 未结 9 1348
孤城傲影
孤城傲影 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条回答
  •  Happy的楠姐
    2020-12-02 10:06

    Here's what I've done (in a separate thread):

    [audioplayer start]
    [audioplayer stop]
    self.audioplayer = audioplayer
    

    [audioplayer prepareToPlay] seems to be an asynchronous method, so you can't be sure when it returns if the audio is in fact ready to play.

    In my case I call start to actually start playing - this appears to be synchronous. Then I stop it immediately. In the simulator anyway I don't hear any sound coming out from this activity. Now that the sound is "really" ready to play, I assign the local variable to a member variable so code outside the thread has access to it.

    I must say I find it somewhat surprising that even on iOS 4 it takes some 2 seconds just to load an audio file that is only 4 seconds in length....

提交回复
热议问题