-1-auidoservices is often used for cue tone playback, the audio has the following four characteristics:
1> length generally not more than 30 seconds, do not need to control the playback process
2> cannot be looped and cannot be paused
3> Cannot play stereo
4> Cannot play remix
-Create prompt Audio auidoservices: The preparatory work
1. Add a System class library. Audiotoolbox.framework
2. import header file in Viewcontroller #import <AudioToolbox/AudioToolbox.h>
A few common methods of tone:
1. Get the path to the sound file
Nsurl *url = [[NSBundle mainbundle] urlforresource:@ "M_03.wav" withextension:nil];
2. Load the sound file to get the corresponding sound ID
Systemsoundid soundid = 0;
Audioservicescreatesystemsoundid (__bridge cfurlref) (URL), &soundid);
3. Play sound
Audioservicesplaysystemsound (Soundid);
4. C-language function for sound playback
The function of sound play is written in C language, so we should pay attention to the difference with OC method in the process of using.
Loading sound files
Audioservicescreatesystemsoundid (Cfurlref infileurl, Systemsoundid *outsystemsoundid)
Releasing sound resources
Audioservicesdisposesystemsoundid (Systemsoundid Insystemsoundid)
Play sound
Audioservicesplaysystemsound (Systemsoundid Insystemsoundid)
Play sound with a little vibration
Audioservicesplayalertsound (Systemsoundid Insystemsoundid)
Simple implementation of the program
#import "RootViewController.h"#import<AudioToolbox/AudioToolbox.h>@interfaceRootviewcontroller ()@end@implementationRootviewcontroller- (ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{ Self=[Super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if(self) {//Custom Initialization } returnSelf ;}- (void) viewdidload{[Super Viewdidload]; [Self Creatuibutton];}-(void) creatuibutton{UIButton* btn=[UIButton Buttonwithtype:uibuttontypecustom]; Btn.frame=cgrectmake ( -, -, -, -); Btn.backgroundcolor=[Uicolor Graycolor]; [BTN Settitle:@"Play"Forstate:uicontrolstatenormal]; [btn Settitlecolor:[uicolor Cyancolor] forstate:uicontrolstatenormal]; [Btn addtarget:self Action: @selector (PRESSBTN:) forcontrolevents:uicontroleventtouchupinside]; [Self.view addsubview:btn];}-(void) Pressbtn: (ID) sender{//1. Get full path because loading sounds requires using cfurlref to load music, you can convert the path using a nsurl that converts to and from it .NSString * pathstr= [[NSBundle Mainbundle]pathforresource:@" Sound"OfType:@"wav"]; Nsurl* Url=[Nsurl Fileurlwithpath:pathstr]; //2. Create a sound ID an ID represents a sound fileSystemsoundid SID; //3. Bind the music file with the ID binding the URL strong to bind the SIDAudioservicescreatesystemsoundid (__bridge cfurlref) URL, &SID); //4. Play SoundAudioservicesplayalertsound (SID); //5. When we need to cancel the sound at the end of the sound playback, we need to use the following method to fix the function nameaudioservicesaddsystemsoundcompletion (SID, nil, nil,finishsound, nil);}//The function is the system method, the function name, the parameter is fixed, cannot modifyvoidFinishsound (Systemsoundid SID,void*finish) { //6. Revoke SIDAudioservicesdisposesystemsoundid (SID);}
Note: When you click Play, you can play the audio file
Getting Started with UI music playback Audioservicesplay