3

I'm trying to get to play a simple sound file through OpenAL by referring this tutorial:

I've created a monolithic code from it to test initially, but cant get the sound to play. I've been trying a lot of stuff but I can't get the sound to play. Any help is much appreciated. Thanks.

Here is my code:

ALCdevice* device;
device = alcOpenDevice(NULL);

ALCcontext* context;
alcCreateContext(device, NULL);
alcMakeContextCurrent(context);

NSString* path = [[NSBundle mainBundle] pathForResource:@"mg" ofType:@"caf"];
NSURL* pathURL = [NSURL fileURLWithPath:path];

AudioFileID audioID;
OSStatus audioStatus = AudioFileOpenURL((CFURLRef)pathURL, kAudioFileReadPermission, 0, &audioID);


UInt32 fileSize = 0;
UInt64 outDataSize;
UInt32 propSize = sizeof(UInt64);
OSStatus res = AudioFileGetProperty(audioID, kAudioFilePropertyAudioDataByteCount, &propSize, &outDataSize);
fileSize = (UInt32)outDataSize;

unsigned char* outData = malloc(fileSize);
OSStatus res2 = AudioFileReadBytes(audioID, false, 0, &fileSize, outData);

AudioFileClose(audioID);

ALuint bufferID;
alGenBuffers(1, &bufferID);
alBufferData(bufferID, AL_FORMAT_STEREO16, outData, fileSize, 44100);

ALuint sourceID = 2;
alGenSources(1, &sourceID);
alSourcei(sourceID, AL_BUFFER, bufferID);
alSourcef(sourceID, AL_PITCH, 1.0f);
alSourcef(sourceID, AL_GAIN, 1.0f);
alSourcei(sourceID, AL_LOOPING, AL_FALSE);
free(outData);
outData = NULL;

alSourcePlay(sourceID);
yuji
  • 16,695
  • 4
  • 63
  • 64
Alterecho
  • 665
  • 10
  • 25
  • I don't know much about openAL, but have you tried the AVAudioPlayer? https://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html#//apple_ref/doc/uid/TP40008067 it may be user to use – Barlow Tucker Feb 10 '12 at 17:39
  • 1
    Yes, i've tried AVAudioPlayer, but i've read its fine only if latency can be tolerated. That is, its not advised for playing sound effects. – Alterecho Feb 11 '12 at 02:59

1 Answers1

0

Just found out! I have been using:

alcCreateContext(device, NULL);

instead of:

context = alcCreateContext(device, NULL);

The alGetError() helped in pointing this out.

Alterecho
  • 665
  • 10
  • 25