Sound: Popularly speaking is short audio, audio files must meet the following conditions
1, the playback time can not exceed 30 seconds
2, the data must be PCM or IMA4 stream format
3. Must be packaged into one of the following three formats: Core audio format (. CAF), waveform audio (. wav), or audio interchange File (. aiff)
1. Introduction of the framework
1 #import <AudioToolbox/AudioToolbox.h>
2. Play sound
1- (void) touchesended: (Nsset *) touches withevent: (Uievent *)Event2 {3 //Statement Soundid4 Systemsoundid Soundid;5Self.soundid =Soundid;6 //get the URL of the sound file7Nsurl *url = [[NSBundle mainbundle] Urlforresource:@"M_10.wav"Withextension:nil];8 //create based on URL9Audioservicescreatesystemsoundid (__bridge cfurlref) URL, &soundid);Ten Audioservicesplaysystemsound (soundid); One};
Memory Skills:
1. Play the core code of the sound, declare the sound ID, and play the sound
1 systemsoundid soundid; 2 audioservicesplaysystemsound (soundid);
2. Assign a value to Soundid in the following sentence
1 audioservicescreatesystemsoundid ((__bridge cfurlref) URL, &soundid);
3. The assignment statement has 2 parameters, one is the sound file resource path, and the other is the previous declaration Soundid
1 nsurl *url = [[NSBundle mainbundle] Urlforresource:@ "m_10.wav" Withextension:nil];
3. After you have finished playing the sound, you should destroy
1 -(void) didreceivememorywarning2{3 [Super Didreceivememorywarning]; 4 Audioservicesdisposesystemsoundid (self.soundid); 5 0 ; 6 }
Refactor the above code to provide a tool class for playing sound effects, refactoring the code from Miss MJ
1.. h Files for tool classes
1 #import<Foundation/Foundation.h>2 3 @interfaceMjaudiotool:nsobject4 5 /**6 * Play Sound7 *8 * @param filename9 */Ten+ (void) PlaySound: (NSString *) filename; One A /** - * Destroy Sound - * the * @param filename - */ -+ (void) Disposesound: (NSString *) filename; - @end
2. Tool class. m file
1 #import "MJAudioTool.h"2 #import<AudioToolbox/AudioToolbox.h>3 4 @implementationMjaudiotool5 6 //dictionary: FileName as key, soundid as value7 //Store all audio IDs8 StaticNsmutabledictionary *_soundiddict;9+ (void) InitializeTen { One_soundiddict =[Nsmutabledictionary dictionary]; A } - -+ (void) PlaySound: (NSString *) filename the { - if(!filename)return; - - //1. Remove the Soundid from the dictionary +Systemsoundid Soundid =[_soundiddict[filename] unsignedlongvalue]; - if(!soundid) {//Create + //Loading sound files ANsurl *url =[[NSBundle Mainbundle] Urlforresource:filename Withextension:nil]; at - if(!url)return; - - //Create a sound ID -Audioservicescreatesystemsoundid (__bridge cfurlref) URL, &soundid); - in //put in a dictionary -_soundiddict[filename] =@ (soundid); to } + - //2. Play the Audioservicesplaysystemsound (soundid); * } $ Panax Notoginseng+ (void) Disposesound: (NSString *) filename - { the if(!filename)return; + ASystemsoundid Soundid =[_soundiddict[filename] unsignedlongvalue]; the if(soundid) { + //Destroy Sound ID - Audioservicesdisposesystemsoundid (soundid); $ $ //Remove from dictionary - [_soundiddict removeobjectforkey:filename]; - } the } - Wuyi @end
3.. m file of the controller
Play sound
1 -(void) touchesended: (Nsset *) touches withevent: (uievent *)event2{ 3 [Mjaudiotool playSound:@ "buyao.wav"]; 4 [Mjaudiotool playSound:@ "m_03.wav"]; 5 }
Destroying sound
1 -(void) didreceivememorywarning2{3 [Super Didreceivememorywarning]; 4 5 [Mjaudiotool disposesound:@ "m_03.wav"]; 6 [Mjaudiotool disposesound:@ "buyao.wav"]; 7 }
IOS Play sound