1

I'm trying to playback an NSArray of sample audio values that I have created in Objective-C. I've generated the values with no problem, but how do I go about playing them back through an iPhone?

Thanks

Max Woolf
  • 3,988
  • 1
  • 26
  • 39

2 Answers2

1

Copy the values out of the NSArray into an C array of appropriately scaled PCM samples. Then you can convert this array to a WAV file by prepending a RIFF header and play this file using an AVAudioPlayer, or you could directly feed the PCM samples into the C array buffers of an Audio Queue or the RemoteIO Audio Unit in their audio callbacks.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
1

I would suggest converting the NSArray to an NSData object, then using the AVAudioPlayer method initWithData:error: (see here) to load as playable audio. AVAudioPlayer has the advantage of being extremely simple to use in relation to Audio Queue and Audio Unit methods.

How you go about converting NSArray to NSData depends on the type of your samples (this SO post might give an idea of how this could be done, although the NSKeyedArchiver archiving process might screw around with your samples). I would suggest, if your sample generation process allows, just creating your samples into an NSData object and skip such a conversion.

Community
  • 1
  • 1