title: MP3 streaming on iOS
body: <p>I want to use OpenAL to play music in an iOS game. The music files are stored in mp3 format and I want to stream them using a buffer queue. I load audio data into the buffers using <em>AudioFileReadPacketData()</em>. However playing the buffers only gives me noise. It works perfectly for caf files, but not for mp3s. Did I miss some vital step in decoding the file?</p>
  
  <p>Code I use to open the sound file:</p>
  
  <pre><code>- (void) openFile:(NSString*)fileName {
     NSBundle *bundle = [NSBundle mainBundle];
     CFURLRef url = (CFURLRef)[[NSURL fileURLWithPath:[bundle pathForResource:fileName ofType:@"mp3"]] retain];
     AudioFileOpenURL(url, kAudioFileReadPermission, 0, &amp;audioFile);
     AudioStreamBasicDescription theFormat;
     UInt32 formatSize = sizeof(theFormat);
     AudioFileGetProperty(audioFile, kAudioFilePropertyDataFormat, &amp;formatSize, &amp;theFormat);  
     freq = (ALsizei)theFormat.mSampleRate;
     CFRelease(url);
  }
  </code></pre>
  
  <p>Code I use to fill in buffers:</p>
  
  <pre><code>- (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, &amp;loadSize, packetDesc, packetsLoaded, &amp;numPackets, data);
      alBufferData(buffer, AL_FORMAT_STEREO16, data, loadSize, freq);
      packetsLoaded += numPackets;
  }
  </code></pre>