2015-04-05 60 views
0

在我的Android應用程序中嗨,我可以在SoundPool中預載我的聲音,然後幾乎沒有延遲地播放它們。現在我在iOS/obj-c上尋找同樣的東西,但我找不到任何類似的東西。iOS Objective C像SoundPool一樣的低延遲音頻播放

我跟着幾個教程,但最終有一個比我預期的更大的滯後和大多數教程建議您將音頻轉換爲像wav或caf這樣的未壓縮格式,但我的MP3已經在14 MB並轉換他們無損音頻導致81 MB的數據,這對我來說太多了。

我試過了預加載文件(就像我在Android的的Soundpool所做的那樣)像顯示在此OAL例如最有前途的事情:

- (bool) preloadUrl:(NSURL*) url seekTime:(NSTimeInterval)seekTime 
{ 
    if(nil == url) 
    { 
     OAL_LOG_ERROR(@"%@: Cannot open NULL file/url", self); 
     return NO; 
    } 

    OPTIONALLY_SYNCHRONIZED(self) 
    { 
     // Bug: No longer re-using AVAudioPlayer because of bugs when using multiple players. 
     // Playing two tracks, then stopping one and starting it again will cause prepareToPlay to fail. 

     bool wasPlaying = playing; 

     [self stopActions]; 

     if(playing || paused) 
     { 
      [player stop]; 
     } 

     as_release(player); 

     if(wasPlaying) 
     { 
      [[NSNotificationCenter defaultCenter] performSelectorOnMainThread:@selector(postNotification:) withObject:[NSNotification notificationWithName:OALAudioTrackStoppedPlayingNotification object:self] waitUntilDone:NO]; 
     } 

     NSError* error; 
     player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
     if(nil == player) 
     { 
      OAL_LOG_ERROR(@"%@: Could not load URL %@: %@", self, url, [error localizedDescription]); 
      return NO; 
     } 

     player.volume = muted ? 0 : gain; 
     player.numberOfLoops = numberOfLoops; 
     player.meteringEnabled = meteringEnabled; 
     player.delegate = self; 
     player.pan = pan; 

     as_release(currentlyLoadedUrl); 
     currentlyLoadedUrl = as_retain(url); 

     self.currentTime = seekTime; 
     playing = NO; 
     paused = NO; 

     BOOL allOK = [player prepareToPlay]; 
     if(!allOK) 
     { 
      OAL_LOG_ERROR(@"%@: Failed to prepareToPlay: %@", self, url); 
     } 
     else 
     { 
      [[NSNotificationCenter defaultCenter] performSelectorOnMainThread:@selector(postNotification:) withObject:[NSNotification notificationWithName:OALAudioTrackSourceChangedNotification object:self] waitUntilDone:NO]; 
     } 
     preloaded = allOK; 
     return allOK; 
    } 
} 

但是,這仍然使大約〜60ms的一個相當大的延時,對於像我這樣的音頻應用來說太過分了。我的音頻文件在開始時沒有任何延遲,因此它必須對代碼進行一些操作。

我嘗試了iPhone 5c上的所有東西。

回答

1

應該能夠創建多個AVAudioPlayer S和呼籲他們prepareToPlay,但我個人喜歡用AVAssetReader保持LPCM的緩衝音頻準備在片刻的通知玩。

+0

好的,打算創建200個播放AVAudioPlayers然後大聲笑我會告訴你它是怎麼回事 – user2875404 2015-04-05 21:37:19

+0

好的隊友非常感謝,把他們放在一個數組中,使得這個工作比我想的要複雜得多成爲;這是現在完美的工作! – user2875404 2015-04-05 22:02:21