2011-01-13 74 views
4

我做了一個iPhone應用程序,它使用OpenAL播放很多聲音。 這些聲音是在MP3中,很重(超過1mn),我把它們流(每個聲音2個緩衝區)以便使用更少的內存。 要管理中斷,我用這個代碼:openAL流媒體&中斷

在OpenALSupport.c文件:

//used to disable openAL during a call 
    void openALInterruptionListener (void *inClientData, UInt32 inInterruptionState) 
    { 
     if (inInterruptionState == kAudioSessionBeginInterruption) 
     { 
      alcMakeContextCurrent (NULL); 
     } 
    } 

    //used to restore openAL after a call 
    void restoreOpenAL(void* a_context) 
    { 
     alcMakeContextCurrent(a_context); 
    } 

在我SoundManager.m文件:

- (void) restoreOpenAL 
    { 
     restoreOpenAL(mContext); 
    } 

    //OPENAL initialization 
    - (bool) initOpenAL 
    { 
     // Initialization 
     mDevice = alcOpenDevice(NULL); 
     if (mDevice) { 
    ... 

      // use the device to make a context 
      mContext=alcCreateContext(mDevice,NULL); 
      // set my context to the currently active one 
      alcMakeContextCurrent(mContext); 

      AudioSessionInitialize (NULL, NULL, openALInterruptionListener, mContext); 

      NSError *activationError = nil; 
      [[AVAudioSession sharedInstance] setActive: YES error: &activationError]; 

      NSError *setCategoryError = nil; 

      [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: &setCategoryError]; 

      ... 
    } 

最後在我的AppDelegate:

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    [[CSoundManager getInstance] restoreOpenAL]; 
    ... 
} 

用這種方法,聲音在呼叫後回來,但流程似乎是pl隨機對話。 有沒有一種管理音頻流中斷的特定方法?我沒有找到任何有關這方面的文章。

感謝您的幫助。

回答

1

好吧,我回答我自己的問題。

我通過我流方法管理錯誤解決了這個問題:

- (void) updateStream 
{ 
ALint processed;  
alGetSourcei(sourceID, AL_BUFFERS_PROCESSED, &processed); 

while(processed--) 
{ 
    oldPosition = position; 

    NSUInteger buffer; 

    alSourceUnqueueBuffers(sourceID, 1, &buffer); 

    //////////////////// 
    //code freshly added 
    ALint err = alGetError(); 
    if (err != 0) 
    { 
     NSLog(@"Error Calling alSourceUnQueueBuffers: %d",err); 
     processed++; 
     //restore old position for the next buffer 
     position = oldPosition; 
     usleep(10000); 
     continue; 
    } 
    ////////////////////  

    [self stream:buffer]; 

    alSourceQueueBuffers(sourceID, 1, &buffer); 

    //////////////////// 
    //code freshly added 
    err = alGetError(); 
    if (err != 0) 
    { 
     NSLog(@"Error Calling alSourceQueueBuffers: %d",err); 
     processed++; 
     usleep(10000); 
     //restore old position for the next buffer 
     position = oldPosition; 
    } 
    /////////////////// 
} 

}

+0

總的想法是非常有幫助,謝謝。但是在其中一種情況下,位置/ oldPosition賦值不是交換的嗎? – 2014-10-13 10:08:09