(1) Sound playback, the core code, mainly to create a player, and then play.
#import "WPSound.h" #import <AVFoundation/AVFoundation.h> @implementation wpsoundstatic nsmutabledictionary *_ soundiddict;+ (void) initialize{_soundiddict=[nsmutabledictionary dictionary];} You cannot use lazy loading because the time class method is given to the user, so you need to set//-(Nsmutabledictionary *) soundiddict{//if (_soundiddict==nil) {//_soundi in class initialization above) Ddict=[nsmutabledictionary dictionary];//}//return _soundiddict;//}+ (void) Soundplay: (NSString *) fileName{if (! FileName) return; Remove Soundid systemsoundid soundid=[_soundiddict[filename] unsignedintvalue]; if (!soundid) {nsurl *url=[[nsbundle mainbundle]urlforresource:filename Withextension:nil]; if (!url) return; Audioservicescreatesystemsoundid ((__bridge cfurlref) URL, &soundid); Put dictionary _soundiddict[filename][email protected] (soundid); } audioservicesplaysystemsound (Soundid);} + (void) Sounddispose: (NSString *) filename{if (fileName) return; Systemsoundid Soundid=[_soundiddict[filename] UnsignedintvalUE]; if (!soundid) return; Audioservicesdisposesystemsoundid (Soundid); Remove [_soundiddict removeobjectforkey:filename] from the dictionary; @end
(2) Music playback, core code. It is important to note that the player created needs to be a global variable.
#import "MusicTool.h" @implementation musictoolstatic nsmutabledictionary *_audioplayerdict;static avaudioplayer *_ audioplayer;+ (void) initialize{_audioplayerdict=[nsmutabledictionary dictionary]; Set Session type Avaudiosession *session=[avaudiosession sharedinstance]; [Session Setcategory:avaudiosessioncategorysoloambient Error:nil]; [Session Setactive:yes Error:nil];} + (Avaudioplayer *) Musicplay: (NSString *) filename{if (!filename) return nil; Remove player Avaudioplayer *audioplayer=_audioplayerdict[filename]; if (!audioplayer) {nsurl *url=[[nsbundle mainbundle]urlforresource:filename Withextension:nil]; if (!url) return nil; Create Audioplayer Avaudioplayer *audioplayer=[[avaudioplayer alloc]initwithcontentsofurl:url Error:NULL]; _audioplayer=audioplayer; [_audioplayer Preparetoplay]; _audioplayer.enablerate=yes; The following settings play speed, can be tested with//_audioplayer.rate=10;//_audioplayerdict[filename]=audioplayer; }if (!_audioplayer.isplaying) {[_audioplayer play]; } return _audioplayer;} + (void) Musicpause: (NSString *) filename{if (!filename) return; Avaudioplayer *audioplayer=_audioplayerdict[filename]; if (audioplayer.isplaying) {[Audioplayer pause]; }}+ (void) Musicstop: (NSString *) filename{if (!filename) return; Avaudioplayer *audioplayer=_audioplayerdict[filename]; if (audioplayer.isplaying) {[Audioplayer stop]; [_audioplayerdict Removeobjectforkey:filename]; }}+ (Avaudioplayer *) currentplayingaudioplayer{for (NSString *filename in _audioplayerdict) {AVAudioPlayer *aud Ioplayer=_audioplayerdict[filename]; if (audioplayer.isplaying) {return audioplayer; }} return nil;} @end
(3) recording, core code.
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface Viewcontroller () @property ( Nonatomic,strong) Avaudiorecorder *recorder, @property (nonatomic,strong) Cadisplaylink *link; @property (Nonatomic, Assign) float silentduration;-(ibaction) Recordstart: (ID) sender;-(ibaction) Recordstop: (ID) sender;@ End@implementation viewcontroller-(Cadisplaylink *) link{if (_link==nil) {_link=[cadisplaylink DisplayLinkWithT Arget:self selector: @selector (update)]; } return _link;} -(void) update{//update the measured value of the recording device [Self.recorder Updatemeters]; Get average decibel float power=[self.recorder averagepowerforchannel:0]; if (power<-30) {self.silentduration+=self.link.duration; if (self.silentduration>=2) {[Self.recorder stop]; [Self.link invalidate]; Self.link=nil; }}else{self.silentduration=0; }}-(void) viewdidload {[Super viewdidload]; Do any additional setup after loading the view, TypIcally from a nib.} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} -(Ibaction) Recordstart: (ID) Sender {nsstring *path=[[nssearchpathfordirectoriesindomains (nsdocumentationdirectory , Nsuserdomainmask, YES) lastobject] stringbyappendingpathcomponent:@ "TEST.CAF"]; Nsurl *url=[nsurl Fileurlwithpath:path]; Nsmutabledictionary *setting=[nsmutabledictionary Dictionary]; Audio format setting[avformatidkey][email protected] (kAudioFormatAppleIMA4); Audio sample rate setting[avsampleratekey][email protected] (8000.0); Number of audio channels setting[avnumberofchannelskey][email protected] (1); Linear audio bit depth setting[avlinearpcmbitdepthkey][email protected] (8); Avaudiorecorder *recorder=[[avaudiorecorder alloc]initwithurl:url settings:setting Error:nil]; Allows measurement of decibel recorder.meteringenabled=yes; cushioning [recorder Preparetorecord]; Recording [Recorder record]; Self.recorder=recordEr Turn on the timer self.silentduration=0; [Self.link Addtorunloop:[nsrunloop Mainrunloop] formode:nsdefaultrunloopmode];} -(Ibaction) Recordstop: (ID) Sender {[Self.link invalidate]; Self.link=nil; [Self.recorder stop];} @end
(3) Play music in the background, set 3 locations.
--Set in APPDELEGATE.M
-(void) Applicationdidenterbackground: (uiapplication *) application { [application Beginbackgroundtaskwithexpirationhandler:nil];}
--Set in Info.plist
--there is also an audio session type set in the program (can be omitted)
+ (void) initialize{ _audioplayerdict=[nsmutabledictionary dictionary]; Set Session type avaudiosession *session=[avaudiosession sharedinstance]; [Session Setcategory:avaudiosessioncategorysoloambient Error:nil]; [Session Setactive:yes Error:nil];}
iOS Dev-118 avfoundation frame: audioservices ... Sound, Avaudioplayer Music, Avaudiorecord recording