Statement of Problem:
I have a collection of sound effects in my app stored as.m4a
files (AAC format, 48 KHz, 16-bit) that I want to play a
For iOS 7, you'd want to look at AVPlayerItem's time-pitch algorithm (audioTimePitchAlgorithm
) called AVAudioTimePitchAlgorithmVarispeed
. Unfortunately this feature is not available on early systems.
The forum link Jack Wu mentions has one suggestion, which involves overriding the AIFF header data directly. This may work, but you will need to have AIFF files since it relies on a specific range of the AIFF header to write into. This also needs to be done before you create the AVAudioPlayer, which means that you can't modify the pitch once it is running.
If you are willing to go to the AudioUnits route, a complete simple solution is probably ~200 lines (note that this assumes the code style that has one function take up to 7 lines with one parameter on each line). There is an Varispeed AudioUnit, which does exactly what you want by locking pitch to rate. You would basically need to look at the API, docs and some sample AudioUnit code to get familiar and then:
kAudioUnitSubType_RemoteIO
) (this outputs to the speaker)kAudioUnitSubType_Varispeed
) to the input of the RemoteIO UnitkAudioUnitSubType_AudioFilePlayer
) unit to read the file and connect it to the varispeed unitNote that there is a TimePitch audio unit which allows independent control of pitch and rate, as well.