I’ve been making an iPhone app for myself at home where I wanted to play sounds. I came across a few issues but I managed to find effective solutions. The first issue being my phone was set to silent but I won’t elaborate that here!
Sound Players and Sound File Formats
There are two framworks I’ve discovered which can be used to control audio:
The AudioServices Class within the AVFoundation Framework which I was using plays sounds which must be: – No longer than 30 seconds in duration – In linear PCM or IMA4 (IMA/ADPCM) format – Packaged in a .caf, .aif, or .wav file.
So my mp3 file was playing in the Simulator fine, but not on my iPhone device, as the simulator uses regular QuickTime for playback.
Convert mp3 to caf
Core Audio Format (caf) is the Apple’s preferred format for audio sound. So it makes sense to convert mp3’s to the caf format.
That’s sound like a repetitive task for automation!
I found the perfect shell script here: How do I use afconvert to convert all the files in a directory from wav to caf?
## ## Shell script to batch convert all files in a directory to caf sound format for iPhone ## Place this shell script a directory with sound files and run it: 'sh converttocaf.sh' ## for f in *; do if [ "$f" != "converttocaf.sh" ] then /usr/bin/afconvert -f caff -d LEI16 $f echo "$f converted" fi done
I might be good to have a ‘watch folder’ that would activate the script when a mp3 is added to it!