IOS System Sound Playback

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.