I want to use OpenAL to play music in an iOS game. Music files are stored in mp3 format, and I want to transfer them using a buffer queue. I load audio data into buffers using AudioFileReadPacketData (). However, playing buffers only gives me noise. It works great for files in a cafe, but not for mp3. Did I miss an important step in decrypting the file?
The code I use to open the sound file is:
- (void) openFile:(NSString*)fileName { NSBundle *bundle = [NSBundle mainBundle]; CFURLRef url = (CFURLRef)[[NSURL fileURLWithPath:[bundle pathForResource:fileName ofType:@"mp3"]] retain]; AudioFileOpenURL(url, kAudioFileReadPermission, 0, &audioFile); AudioStreamBasicDescription theFormat; UInt32 formatSize = sizeof(theFormat); AudioFileGetProperty(audioFile, kAudioFilePropertyDataFormat, &formatSize, &theFormat); freq = (ALsizei)theFormat.mSampleRate; CFRelease(url); }
The code I use to fill buffers is:
- (void) loadOneChunkIntoBuffer:(ALuint)buffer { char data[STREAM_BUFFER_SIZE]; UInt32 loadSize = STREAM_BUFFER_SIZE; AudioStreamPacketDescription packetDesc[STREAM_PACKETS]; UInt32 numPackets = STREAM_PACKETS; AudioFileReadPacketData(audioFile, NO, &loadSize, packetDesc, packetsLoaded, &numPackets, data); alBufferData(buffer, AL_FORMAT_STEREO16, data, loadSize, freq); packetsLoaded += numPackets; }
iphone openal
Marvin killing
source share