IOS Audio Learning

Source: Internet
Author: User

Audioservicesaddsystemsoundcompletion (ksystemsoundid_vibrate, NULL, NULL, systemaudiocallback, NULL);

Audioservicesremovesystemsoundcompletion (ksystemsoundid_vibrate);

The purpose of these two interfaces is to bind and cancel the specified Soundid corresponding callback method; Ksystemsoundid_vibrate is the Soundid type, and its callback method is the Soundid, Use this ID anywhere to execute audioservicesplaysystemsound (Xxxsoundid) will be called to the callback method. Once the Remove method is called to cancel the callback, the same ID is used anywhere to execute audioservicesplaysystemsound (Xxxsoundid) without invoking the callback.

System Vibration

1. Join the Audiotoolbox.framework framework

2. Add header file #import <AudioToolbox/AudioToolbox.h>

3. use the Audioservicesplaysystemsound interface for sound and vibration playback

For example:

1. Play and stop vibration

Play vibrate

Audioservicesplaysystemsound (ksystemsoundid_vibrate);

Stop shaking

Audioservicesremovesystemsoundcompletion (ksystemsoundid_vibrate);

IPod Touch does not support vibration, if it is not valid with the above code

2. Play a specific sound

static Systemsoundid soundidtest = 0;

NSString * Path = [[NSBundle mainbundle] pathforresource:@ "test" oftype:@ "wav"];

if (path) {

Audioservicescreatesystemsoundid (cfurlref) [Nsurl Fileurlwithpath:path], &soundidtest);

}

Audioservicesplaysystemsound (soundidtest);

4. A method of constant vibration:

Define a callback function that vibrates again at the end of the vibration

void Myaudioservicessystemsoundcompletionproc (Systemsoundid ssid,void *clientdata)

{

bool* ishouldkeepbuzzing = clientdata;

if (*ishouldkeepbuzzing) {audioservicesplaysystemsound (ksystemsoundid_vibrate);

} else {

Unregister, so we don ' t get called again ...

Audioservicesremovesystemsoundcompletion (ksystemsoundid_vibrate);

}

}

The following code is called:

BOOL ishouldkeepbuzzing = YES;

Audioservicesaddsystemsoundcompletion (

Ksystemsoundid_vibrate,

  null,                                                                                                         

  null,                                                                                                                    

Myaudioservicessystemsoundcompletionproc,

&ishouldkeepbuzzing);

Audioservicesplaysystemsound (ksystemsoundid_vibrate);

For simple, no-mix audio, the Avaudio Toolbox Framework provides a simple C-language-style audio service. You can use the Audioservicesplaysystemsound function to play simple sounds. Here are a few rules to follow:

1. Audio length less than 30 seconds

2. The format can only be PCM or IMA4

3. File must be stored as. CAF,. AIF, or. wav format

4. Simple audio cannot be played from memory, but only by disk files

In addition to the restrictions on simple audio, you have virtually no control over how audio is played. Once the audio is played, it starts immediately and is the volume playback that is set by the current phone user. You won't be able to loop through the sound or control the stereo effect. However, you can still set a callback function to be called at the end of the audio playback, so you can clean up the audio object and notify your program to end.

Directly on the code:

  1. #import <AudioToolbox/AudioToolbox.h>
  2. #import <CoreFoundation/CoreFoundation.h>
  3. This function is called when the audio is finished playing
  4. static void soundfinished (Systemsoundid soundid,void* sample) {
  5. /* Play all over, so release all resources */
  6. Audioservicesdisposesystemsoundid (sample);
  7. Cfrelease (sample);
  8. Cfrunloopstop (Cfrunloopgetcurrent ());
  9. }
  10. Main loop
  11. int main () {
  12. /* System audio ID to register the sound we are going to play */
  13. Systemsoundid Soundid;
  14. nsurl* sample = [[Nsurl alloc]initwithstring:@ "Sample.wav"];
  15. Osstatus err = Audioservicescreatesystemsoundid (sample, &soundid);
  16. if (err) {
  17. NSLog (@ "Error occurred assigning system sound!");
  18. return-1;
  19. }
  20. /* Add a callback at the end of the audio */
  21. Audioservicesaddsystemsoundcompletion (soundid, NULL, NULL, soundfinished,sample);
  22. /* Start playing */
  23. Audioservicesplaysystemsound (Soundid);
  24. Cfrunlooprun ();
  25. return 0;
  26. }

Personally feel that this audio service is a bit of a chicken, but it certainly has its use, such as we want to play a custom warning tone or message hint, with the audio service is certainly more than other methods to save resources.

1, first introduce the playback system audio. This system sound is relatively short, only a few seconds, can not repeat, immediately play the kind of sound. such as SMS ringtones. Beep,boun ...

DECLARE two variables:

Cfurlref Soundfileurlref; Sound file path

Systemsoundid Soundfileobject; Sound ID

Then define the function to use.

-(void) soundplay{

2 {

3 Cfbundleref mainbundle = Cfbundlegetmainbundle ();

4 Soundfileurlref = Cfbundlecopyresourceurl (Mainbundle,

5 Cfstr ("Pageflip"),

6 Cfstr ("AIF"),

7 NULL);

8

9 Audioservicescreatesystemsoundid (Soundfileurlref, &soundfileobject);

Ten audioservicesaddsystemsoundcompletion (Soundfileobject,

NULL,//uses the main run loop

NULL,//uses Kcfrunloopdefaultmode

Soundfinished,//The name of our custom callback function

NULL/For the user data, but we don ' t need to do the UST Pass NULL

15);

16

Audioservicesplaysystemsound (Soundfileobject);

18}

19

20//This function is C-style.

$ void soundfinished (Systemsoundid sound_id, void* user_data) {

Audioservicesdisposesystemsoundid (sound_id);

23}

"Pageflip.aif" is the full name of the file.

Audioservicescreatesystemsoundid is registered sound,

Audioservicesplaysystemsound playing Sound

Audioservicesaddsystemsoundcompletion listen to the completion of the event method, you have to remember the sound when done, because it will occupy resources.

Audioservicesdisposesystemsoundid Clear Sound

IOS Audio Learning

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.