iOS Platform Audio Development

Source: Internet
Author: User

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

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.