Support for sound is essential in the development of iPhone applications or games. In a few of the applications I've done, each one involves sound effects, so here's a simple generalization, many of which are quoted from the IPhone Application Programming Guide (which requires an Apple ID to open the link), plus some practical experience.
IPhone OS primarily provides system sound services to play audio
System Sound Services is the lowest and simplest voice playback service, call Audioservicesplaysystemsound This method can play some simple audio files, using this method is only suitable for playing some very small hints or warning tones, Because it has a lot of limitations:
Sound length less than 30 seconds
In linear PCM or IMA4 (IMA/ADPCM) format
Package as. CAF,. AIF, or. wav files
Cannot control the progress of playback
Play sound immediately after calling method
No loop playback and stereo control
In addition, it can call the system's vibration function, the method is also very simple. The specific code can refer to the official example Syssound, but the official example has only a few simple uses, from which we find that the Audioservicesaddsystemsoundcompletion method can be used to add CallBack functions for audio playback. With the CallBack function, we can solve a lot of problems, such as overcoming the problem that System sound Services itself does not support loop playback. The following code can implement a background music that loops through the program:
View Plaincopy to Clipboardprint?
static void CompletionCallback (Systemsoundid myssid) {
Play again after sound play completion
Audioservicesplaysystemsound (MYSSID);
}
-(void) PlaySound {
Get the main bundle for the app
Cfbundleref Mainbundle;
Systemsoundid Soundfileobject;
Mainbundle = Cfbundlegetmainbundle ();
Get the URL to the "sound" file to play
Cfurlref soundfileurlref = Cfbundlecopyresourceurl (
Mainbundle,
CFSTR ("Background"),
CFSTR ("wav"),
Null
);
Create A system sound object representing the sound file
Audioservicescreatesystemsoundid (
Soundfileurlref,
&soundfileobject
);
ADD Sound Completion callback
Audioservicesaddsystemsoundcompletion (soundfileobject, NULL, NULL,
CompletionCallback,
(void*) self);
Play the Audio
Audioservicesplaysystemsound (Soundfileobject);
}
static void CompletionCallback (Systemsoundid myssid) {
Play again after sound play completion
Audioservicesplaysystemsound (MYSSID);
}
-(void) PlaySound {
Get the main bundle for the app
Cfbundleref Mainbundle;
Systemsoundid Soundfileobject;
Mainbundle = Cfbundlegetmainbundle ();
Get the URL to the "sound" file to play
Cfurlref soundfileurlref = Cfbundlecopyresourceurl (
Mainbundle,
CFSTR ("Background"),
CFSTR ("wav"),
Null
);
Create A system sound object representing the sound file
Audioservicescreatesystemsoundid (
Soundfileurlref,
&soundfileobject
);
ADD Sound Completion callback
Audioservicesaddsystemsoundcompletion (soundfileobject, NULL, NULL,
CompletionCallback,
(void*) self);
Play the Audio
Audioservicesplaysystemsound (Soundfileobject);
}
iOS Platform Audio Development