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:
- #import <AudioToolbox/AudioToolbox.h>
- #import <CoreFoundation/CoreFoundation.h>
- This function is called when the audio is finished playing
- static void soundfinished (Systemsoundid soundid,void* sample) {
- /* Play all over, so release all resources */
- Audioservicesdisposesystemsoundid (sample);
- Cfrelease (sample);
- Cfrunloopstop (Cfrunloopgetcurrent ());
- }
- Main loop
- int main () {
- /* System audio ID to register the sound we are going to play */
- Systemsoundid Soundid;
- nsurl* sample = [[Nsurl alloc]initwithstring:@ "Sample.wav"];
- Osstatus err = Audioservicescreatesystemsoundid (sample, &soundid);
- if (err) {
- NSLog (@ "Error occurred assigning system sound!");
- return-1;
- }
- /* Add a callback at the end of the audio */
- Audioservicesaddsystemsoundcompletion (soundid, NULL, NULL, soundfinished,sample);
- /* Start playing */
- Audioservicesplaysystemsound (Soundid);
- Cfrunlooprun ();
- return 0;
- }
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