openAL 流媒体和打扰

发布于 2024-10-11 11:42:01 字数 1758 浏览 4 评论 0原文

我制作了一个 iPhone 应用程序,它使用 OpenAL 来播放许多声音。 这些声音是 mp3 格式,相当重(超过 100 万),我对它们进行流式处理(每个声音 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];
    ...
}

使用此方法,声音在呼叫后返回,但流程似乎是随机播放的。 有没有一种特定的方法来管理流声音的中断?我没有找到任何相关的文章。

感谢您的帮助。

I made an iphone application which uses OpenAL to play many sounds.
These sounds are in mp3, quite heavy (more than 1mn)and I stream them (2 buffers per sound) in order to use less memory.
To manage interruptions, I use this code :

In OpenALSupport.c file :

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

In my SoundManager.m file :

 - (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];

            ...
    }

And finally in my AppDelegate :

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

With this method the sounds are back after a call , but flows seem to be played randomly.
Is there a specific way to manage interruption with streaming sounds ? I don't find any article about that.

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉絮湮 2024-10-18 11:42:01

好吧,我回答我自己的问题。

我通过管理流方法上的错误解决了这个问题:

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

}

Ok, I answer to my own question.

I solved the problem by managing error on my streaming method :

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文