iphone - openAL 流媒体和中断

标签 iphone streaming openal interrupt-handling

我制作了一个 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];
    ...
}

使用此方法,声音会在通话后恢复,但流程似乎是随机播放的。 是否有特定的方法来管理流声音的中断?我没有找到任何相关文章。

感谢您的帮助。

最佳答案

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

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

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

}

关于iphone - openAL 流媒体和中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4678709/

相关文章:

ios - iPhone 和 iPad 上的 View 放置方式不同

c++ - 如何让 mingw64 链接到 Windows 上的 OpenAL

java - OpenAL中是否可以直接选择录音设备?

ios - 您如何使用 iPhone AVAudioPlayer 控制播放级别(分贝?)?或者我需要使用不同的 API 吗?

ios - 了解保留计数以及在其中访问 self 的 block 。

iphone - 如何使用蓝牙测量两个iPhone设备之间的距离?

ios - 如何将 UIDatePicker 中的日期和时间设置为 12 小时格式的 UITextField

php - 在 PHP 中流式传输

android - MediaPlayer,只有视频 m3u8 HTML 流工作

JavaScript:用于流媒体的简单 SoundCloud API 示例无法在 Safari 中正常工作