There are two types of audio: 1, sound 2, music playback requires two frames:avfoundation.frameworkaudiotoolbox.framework
1 #import "ViewController.h" 2 #import<AVFoundation/AVFoundation.h>3 @interfaceViewcontroller ()4@property (nonatomic,assign) Systemsoundid Soundid;5 6 @end 7 8 @implementationViewcontroller9 Ten- (void) Viewdidload { One[Super Viewdidload]; A } - - //need a lazy load-otherwise no sound will report err-50 error the-(systemsoundid) Soundid - { - if(!_soundid) - { +Nsurl *url = [[NSBundle mainbundle] Urlforresource:@"M_03.wav"Withextension:nil]; -Audioservicescreatesystemsoundid (__bridge cfurlref _nonnull) (URL), &_soundid); + } A return_soundid; at } - --(void) touchesended: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event - { - //Loading sound files (short audio) 1 audio files-1 Soundid - //Systemsoundid Soundid; in //Nsurl *url = [[NSBundle mainbundle] urlforresource:@ "M_03.wav" withextension:nil]; - //Audioservicescreatesystemsoundid ((__bridge cfurlref _nonnull) URL, &soundid); to + //get the sound ID to play -Audioservicesplaysystemsound (self.soundid); the * } $ Panax Notoginseng-(void) didreceivememorywarning - { the[Super didreceivememorywarning]; + AAudioservicesdisposesystemsoundid (Self.soundid);//There's a creation, there's destruction . theSelf.soundid =0; +}Viewcontroller Code
But since each soundid corresponds to one audio, it is tedious to make multiple soundid when there is more than one audio, in order to simplify, create a tool class
Audiotool
+ (void) PlaySound: (NSString *) fileName
{
Systemsoundid Soundid;
Nsurl *url = [[NSBundle mainbundle] Urlforresource:filename Withextension:nil];
Audioservicescreatesystemsoundid ((__bridge cfurlref _nonnull) URL, &soundid);
}
Since the destruction is required to get the previously created Soundid to destroy, so to take the soundid out as a global (you might think of member variables at the moment-but the class method is not allowed to access member variables, so get a global static)
+ (void) Disposesound: (NSString *) fileName
{
}
1 #import "AudioTool.h"2 #import<AVFoundation/AVFoundation.h>3 @implementationAudiotool4 5 //dictionary: FileName as key, soundid as value6 //Store all audio IDs7 StaticNsmutabledictionary *_soundiddict;8+(void) Initialize9 {Ten_soundiddict =[Nsmutabledictionary dictionary]; One } A -+(void) PlaySound: (NSString *) FileName - { the if(!filename)return; - -Systemsoundid Soundid =[_soundiddict[filename] unsignedintvalue]; - if(!soundid)//Create + { -Nsurl *url =[[NSBundle Mainbundle] Urlforresource:filename Withextension:nil]; + if(!url)return; A at //1----Creating a sound ID -Audioservicescreatesystemsoundid (__bridge cfurlref _nonnull) URL, &soundid); - - //put in a dictionary -_soundiddict[filename] =@ (soundid); - } in - //2-----play--audioservicesplaysystemsound play short audio long audio-audioplayer to Audioservicesplaysystemsound (soundid); + } - the+(void) Disposesound: (NSString *) FileName * { $ if(!filename)return;Panax NotoginsengSystemsoundid Soundid =[_soundiddict[filename] unsignedintvalue]; - if(Soundid)//If there is soundid destruction the { + Audioservicesdisposesystemsoundid (soundid); A the[_soundiddict Removeobjectforkey:filename];//Remove from dictionary + } -}Audiotool
1 #import "ViewController.h"2 //#import <AVFoundation/AVFoundation.h>3 #import "AudioTool.h"4 @interfaceViewcontroller ()5 6 @end7 8 @implementationViewcontroller9 Ten- (void) Viewdidload { One [Super Viewdidload]; A } - - the-(void) touchesended: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event - { -NSString *filename = [NSString stringWithFormat:@"M_%02d.wav", Arc4random_uniform ( -)+3]; - + [Audiotool playsound:filename]; - } + A-(void) didreceivememorywarning at { - [Super didreceivememorywarning]; - - //[Audiotool disposesound:@ "M_03.wav"]; -}Viewcontroller Code
01--Sound (short audio)