There are two ways to play sounds in iOS:
* Play compressed sound files, such as MP3 files, generally use the method mentioned in the playback sound , suitable for playing longer, such as more than 30 seconds, Avaudioplayer can provide more control features, such as pause and so on
* play the system sound, such as the crash of the page, should use the method of playing the system sound, because the compressed sound file than playing WAV and other non-compressed sound files before playing consumes more processor resources, such sound also to play frequently, and, because the system sound files are smaller, generally less than 30 seconds, So even if you compress the format, you won't save much storage space.
Play system sound: First to import the system framework <AudioToolbox/AudioToolbox.h>
Play the system sound, the basic idea is:
* before playback, such as the controller initialization stage, register the system sound, and save a system sound ID
* When you need to play the system sound, just call the system service global method, incoming system sound ID can be
After we understand the basic idea, we can go to the code:
*.h file
+ (void) Initsystemsoundwithname: (NSString *) soundname soundtype: (NSString *) soundtype{[Zkaudiotoolbox Initaudiotoolbox]; if (Audiotool) {//Get audio path nsstring *path=[nsstring stringwithformat:@ "/system/library/audio/uisounds/%@.%@", Soundname,soundtype]; if (path) {Osstatus error = Audioservicescreatesystemsoundid (__bridge cfurlref) [Nsurl fileurlwithpath:path],& Amp;audiotool->sound); if (Error! = Kaudioservicesnoerror) {//Gets the sound of the time, an error occurs audiotool->sound=nil; }//Get a custom sound//NSString *thesoundfilepath = [[NSBundle Mainbundle] Pathforresource:sound Name Oftype:soundtype]; Music file path//Cfurlref Thesoundurl = (__bridge cfurlref) [Nsurl fileurlwithpath:thesoundfilepath];//Aud Ioservicescreatesystemsoundid (Thesoundurl, &audiotool->sound); Start playing sound audioservicesplaysystemsound (Audiotool->sound); } }}
As above can play the sound of the normal, written in the form of a single case, you can easily invoke multiple pages!!!
IOS System Sound Playback