IOS streaming video

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; } 
+7
iphone openal
source share
1 answer

Because you read bytes of MP3 data and treat them like PCM data.

You almost certainly want AudioFileReadPacketData() . EDIT: Except it still gives you MP3 data; it simply passes it into packets and (possibly) parses the packet headers.

If you don't need OpenAL, AVAudioPlayer is probably the best way to go (according to the Guide to Multimedia Programming , there is also Audio Queue Services if you want more control).

If you really need to use OpenAL, according to TN2199 you will need to convert it to PCM in your own byte order. See oalTouch / Classes / MyOpenALSupport.c for an example of using Extended Audio File Services for this. Please note that TN2199 says that the format โ€œshould ... not use hardware decompressionโ€ - according to the Media Programming Guide, software decoding is supported for everyone except HE-AAC with OS 3.0. Also note that MP3 decoding software can use a significant amount of processor time.

Alternatively, explicitly convert audio using AudioConverter or (possibly) AudioUnit with kAudioUnitSubType_AUConverter. If you do, it may be worth it to parse everything once and store it in memory to minimize overhead.

+1
source share

All Articles