4

OpenALを使ってたくさんの音を鳴らすiPhoneアプリを作りました。これらのサウンドはmp3であり、非常に重く(1分以上)、メモリの使用量を減らすためにストリーミングします(サウンドごとに2つのバッファー)。中断を管理するために、私はこのコードを使用します:

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];
    ...
}

この方法では、呼び出し後にサウンドが返されますが、フローはランダムに再生されているように見えます。ストリーミングサウンドの中断を管理する特定の方法はありますか?それについての記事は見つかりません。

ご協力いただきありがとうございます。

4

1 に答える 1

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;
    }
    ///////////////////
}

}

于 2011-01-13T16:26:29.403 に答える