Search code examples
ipadios7openal

Getting an audio device with OpenAL


I'm trying to use OpenAL for an IOS game I'm working on, but having an issue opening the audio device. Specifically, when I call the function alcOpenDevice(NULL), I get 'NULL' in return. This is causing issues, of course, but I can't tell what I'm doing wrong.

I'm new to OpenAL, so I've been looking at a couple guides here and here to see what I need to do. If I download their sample projects and test 'em, they both work fine. If I copy their files into my project, and ignore the files I made, they still work fine. I'm assuming something got lost in translation when I started rebuilding the code for use in my project. Asking around and searching online hasn't given me any leads though, so I'm hoping someone here could put me on the right track.

Here's the actual setup code I'm using in my AudioPlayer.m

- (void)setup {
    audioSampleBuffers = [NSMutableDictionary new];
    audioSampleSources = [NSMutableArray new];


    [self setupAudioSession];
    [self setupAudioDevice];
    [self setupNotifications];
}

- (BOOL)setupAudioSession {

//    // This has been depricated.
//
//    /* Setup the Audio Session and monitor interruptions */
//    AudioSessionInitialize(NULL, NULL, AudioInterruptionListenerCallback, NULL);
//
//    /* Set the category for the Audio Session */
//    UInt32 session_category = kAudioSessionCategory_MediaPlayback;
//    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(session_category), &session_category);
//
//    /* Make the Audio Session active */
//    AudioSessionSetActive(true);

    BOOL success = NO;
    NSError *error = nil;

    AVAudioSession *session = [AVAudioSession sharedInstance];

    success = [session setCategory:AVAudioSessionCategoryPlayback error:&error];
    if (!success) {
        NSLog(@"%@ Error setting category: %@", NSStringFromSelector(_cmd), [error localizedDescription]);

        return success;
    }

    success = [session setActive:YES error:&error];
    if (!success) {
        NSLog(@"Error activating session: %@", [error localizedDescription]);
    }

    return success;
}

- (BOOL)setupAudioDevice {
    // 'NULL' uses the default device.
    openALDevice = alcOpenDevice(NULL); // Returns 'NULL'
    ALenum error = alGetError(); // Returns '0'
    NSLog(@"%i", error);

    if (!openALDevice) {
        NSLog(@"Something went wrong setting up the audio device.");
        return NO;
    }


    // Create a context to use with the device, and make it the current context.
    openALContext = alcCreateContext(openALDevice, NULL);
    alcMakeContextCurrent(openALContext);

    [self createAudioSources];

    // Setup was successful
    return YES;
}

- (void)createAudioSources {
    ALuint sourceID;
    for (int i = 0; i < kMaxConcurrentSources; i++) {
        // Create a single source.
        alGenSources(1, &sourceID);
        // Add it to the array.
        [audioSampleSources addObject:[NSNumber numberWithUnsignedInt:sourceID]];
    }
}

Note: I'm running IOS 7.1.1 on a new iPad air, and using Xcode 5.1.1. This issue has been confirmed on the iPad, my simulator, and an iPod touch.


Solution

  • Alright, so I ended up starting over in a fresh project with a copy-pasted version of AudioSamplePlayer from the first sample project I linked. -It worked.

    I then edited it step by step back to the format I had set up in my project. -It still works!

    I still don't know what I did wrong the first time, and I'm not sure it was even in my audio player anymore, but It's running now. I blame gremlins.

    ...maybe alien surveillance.