IOS-audio playback audioservices

Source: Internet
Author: User

Sound support is essential in the development of iPhone applications or games. Each of the applications I have done involves sound effects. Therefore, many of them refer
Application Programming Guide (requires an Apple ID to open the link), with some practical experience.

IPhone OS mainly provides the following methods for playing audio:

  • System sound services
  • Avaudioplayer class
  • Audio queue services
  • Openal


About system sound services

System sound services is the most basic and simple sound playback service. You can call audioservicesplaysystemsound to play some simple audio files, this method is only suitable for playing small prompts or warning sounds, because it has many restrictions:


The sound length must be less than 30 seconds.

■ In linear PCM or ima4 (IMA/ADPCM) Format

■ Package it into. CAF,. AIF, or. wav files.

■ The playback progress cannot be controlled.

■ Play the sound immediately after calling the Method

■ No loop playback and stereo Control

In addition, it can also call the system vibration function, the method is also very simple. For specific code, refer to the official syssound example, but the official example only has some simple usage. From this document, we find that you can add audio playback using the audioservicesaddsystemsoundcompletion method.
With the callback function, we can solve many problems. For example, we can overcome the problem that system sound services does not support loop playback. The following code implements a background music loop in the program:

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);      }

Another section:

# Include <audiotoolbox/audiotoolbox. h> # include <corefoundation/corefoundation. h> // define a callback to be called when the sound is finished // playing. useful when you need to free memory after playing. static void mycompletioncallback (systemsoundid myssid, void * myurlref) {response (myssid); cfrelease (myurlref); // release cfrunloopstop (cfrunloopgetcurrent () created by wait ());} int main (INT argc, const char * argv []) {// set up the pieces needed to play a sound. systemsoundid myssid; cfurlref myurlref; myurlref = cfurlcreatewithfilesystempath (kcfallocatordefault, cfstr ("http://www.cnblogs.com/ComedyHorns.aif"), kcfurlposixpathstyle, false ); // create a system sound ID to represent the sound file osstatus error = audioservicescreatesystemsoundid (myurlref, & myssid); // register the sound completion callback. // again, useful when you need to free memory after playing. audioservicesaddsystemsoundcompletion (myssid, null, null, mycompletioncallback, (void *) myurlref); // play the sound file. audioservicesplaysystemsound (myssid); // invoke a run loop on the current thread to keep the application // running long enough for the sound to play; the sound completion // callback later stops this run loop. cfrunlooprun (); Return 0 ;}

In addition to restrictions on simple audio, you cannot control the way the audio is played. Once the audio is played, it starts immediately and is the volume set by the current phone user. You will not be able to play audio cyclically or control the effect of a stereo. However, you can set a callback function that is called at the end of audio playback. In this way, you can clean up the audio objects and notify your program to finish playing.

I personally think this audio service is not very powerful, but it is very suitable in some cases. For example, we want to play a custom warning sound or message prompt, the use of audio services certainly saves resources than other methods.


Related Article

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.