For simple, no-mix audio, the Avaudio Toolbox Framework provides a simple C-language-style audio service. You can use the Audioservicesplaysystemsound function to play simple sounds. Here are a few rules to follow:
1. Audio length less than 30 seconds
2. The format can only be PCM or IMA4
3. File must be stored as. CAF,. AIF, or. wav format
4. Simple audio cannot be played from memory, but only by disk files
In addition to the restrictions on simple audio, you have virtually no control over how audio is played. Once the audio is played, it starts immediately and is the volume playback that is set by the current phone user. You won't be able to loop through the sound or control the stereo effect. However, you can still set a callback function to be called at the end of the audio playback, so you can clean up the audio object and notify your program to end.
Directly on the code:
- #Import <AudioToolbox/AudioToolbox.h>
- #Import <CoreFoundation/CoreFoundation.h>
- This function is called when the audio is finished playing
- static void soundfinished (Systemsoundid soundid,void* sample) {
- / * Play all over, so release all resources * /
- Audioservicesdisposesystemsoundid (sample);
- Cfrelease (sample);
- Cfrunloopstop (Cfrunloopgetcurrent ());
- }
- Main loop
- int main () {
- / * System audio ID to register the sound we are going to play * /
Audioservicesplaysystemsound Audio Service-Standby