2011-01-13 107 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