-(AVAudioRecorder *)recoder { if (_recoder == nil) { //1.建立存放录音文件的地址 NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *filePath = [path stringByAppendingPathComponent:@"recoder.caf"]; NSURL *url = [NSURL URLWithString:filePath]; //iOS8必须实例前设置开启代理 AVAudioSession *session = [AVAudioSession sharedInstance]; NSError *setCategoryError = nil; [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&setCategoryError]; if(setCategoryError){ NSLog(@"%@", [setCategoryError description]); } //2.建立录音对象 self.recoder = [[AVAudioRecorder alloc] initWithURL:url settings:nil error:nil]; //3.准备录音 [self.recoder prepareToRecord]; } return _recoder; } - (IBAction)start:(UIButton *)sender { //4.录音 [self.recoder record]; } - (IBAction)stop:(UIButton *)sender { //5.中止录音 [self.recoder stop]; }
/*** AudioServicesPlaySystemSound若是系统静音, 则用户提示就没有什么效果 AudioServicesPlayAlertSound若是静音会震动,不然声音提示 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);单一的震动 */ 播放声音能够用二者,播放提醒只能用,播放震动 //振动 var soundID = SystemSoundID(kSystemSoundID_Vibrate) AudioServicesPlaySystemSound(soundID) ========================播放音效公用代码===================== //1.建立SystemSoundID,根据音效文件来生成 SystemSoundID soundID = 0; //2.根据音效文件,来生成SystemSoundID NSURL *url = [[NSBundle mainBundle] URLForResource:@"win.aac" withExtension:nil]; CFURLRef urlRef = (__bridge CFURLRef)(url); AudioServicesCreateSystemSoundID(urlRef, &soundID); //3.播放印象 AudioServicesPlaySystemSound(soundID); //AudioServicesPlayAlertSound(soundID); =================封装播放音效======================== static NSMutableDictionary *_soundIDs; + (NSMutableDictionary *)soundIDs { if (!_soundIDs) { _soundIDs = [NSMutableDictionary dictionary]; } return _soundIDs; } + (void)playAudioWithFilename:(NSString *)filename { if (filename == nil) { return; } //1.从字典中取出音效ID SystemSoundID soundID = [[self soundIDs][filename] unsignedIntValue]; //2.判断ID是否为nil if (!soundID) { NSLog(@"建立新的soundID"); NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil]; if (!url) { return; } //创新音效ID AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID); //将音效ID添加到字典中 [self soundIDs][filename] = @(soundID); } //3.播放音效 AudioServicesPlaySystemSound(soundID); } //内存警告的是否释放播放音效 + (void)disposeAudioWithFilename:(NSString *)filename { // 0.判断文件名是否为nil if (filename == nil) { return; } // 1.从字典中取出音效ID SystemSoundID soundID = [[self soundIDs][filename] unsignedIntValue]; if (soundID) { // 2.销毁音效ID AudioServicesDisposeSystemSoundID(soundID); // 3.从字典中移除已经销毁的音效ID [[self soundIDs] removeObjectForKey:filename]; } }
//若是须要播放多首音乐,能够参考播放音效的操做,将id做为value,name做为key。 -(AVAudioPlayer *)player { if (_player == nil) { //取出资源的url NSURL *url = [[NSBundle mainBundle] URLForResource:@"a.mp3" withExtension:nil]; //建立播放器 NSError *error = nil; self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; //准备播放 [self.player prepareToPlay]; } return _player; } - (IBAction)start:(id)sender { [self.player play]; } - (IBAction)stop:(id)sender { [self.player stop]; } - (IBAction)pause:(id)sender { [self.player pause]; //中止没法,只能暂停,能够清空播放器对象 self.player = nil; }