录制音频
想要在ios设备上录制音频,可以使用AVAudioRecorder类,确保你已经将CoreAudio.framework 库添加到目标文件中
//// ViewController.m// 录制音频//// Created by Rio.King on 13-11-2.// Copyright (c) 2013年 Rio.King. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; NSError *error = nil; NSString *pathAsString = [self audioRecordingPath]; NSURL *audioRecordingURL = [NSURL fileURLWithPath:pathAsString]; self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:audioRecordingURL settings:[self audioRecordingSettings] error:&error]; if (self.audioRecorder != nil) { self.audioRecorder.delegate = self; /*prepare the recorder and then start the recording*/ if ([self.audioRecorder prepareToRecord] && [self.audioRecorder record]) { NSLog(@"Successfully started to record."); /*after five seconds, let's stop the recording process*/ [self performSelector:@selector(stopRecordingOnAudioRecorder:) withObject:self.audioRecorder afterDelay:10.0f]; }else{ NSLog(@"Failed to record."); self.audioRecorder = nil; } }else{ NSLog(@"failed to create an instance of the audio recorder."); }}-(NSString *)audioRecordingPath{ NSString *result = nil; NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsFolder = [folders objectAtIndex:0]; result = [documentsFolder stringByAppendingPathComponent:@"Recording.m4a"]; return result;}- (NSDictionary *) audioRecordingSettings{ NSDictionary *result = nil; /* Let's prepare the audio recorder options in the dictionary. Later we will use this dictionary to instantiate an audio recorder of type AVAudioRecorder */ NSMutableDictionary *settings = [[NSMutableDictionary alloc] init]; [settings setValue:[NSNumber numberWithInteger:kAudioFormatAppleLossless] forKey:AVFormatIDKey]; [settings setValue:[NSNumber numberWithFloat:44100.0f] forKey:AVSampleRateKey]; [settings setValue:[NSNumber numberWithInteger:1] forKey:AVNumberOfChannelsKey]; [settings setValue:[NSNumber numberWithInteger:AVAudioQualityLow] forKey:AVEncoderAudioQualityKey]; result = [NSDictionary dictionaryWithDictionary:settings]; return result;}- (void) stopRecordingOnAudioRecorder:(AVAudioRecorder *)paramRecorder{ /* Just stop the audio recorder here */ [paramRecorder stop];}- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{ if (flag){ NSLog(@"Successfully stopped the audio recording process."); /* Let's try to retrieve the data for the recorded file */ NSError *playbackError = nil; NSError *readingError = nil; NSData *fileData = [NSData dataWithContentsOfFile:[self audioRecordingPath] options:NSDataReadingMapped error:&readingError]; /* Form an audio player and make it play the recorded data */ self.audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:&playbackError]; /* Could we instantiate the audio player? */ if (self.audioPlayer != nil){ self.audioPlayer.delegate = self; /* Prepare to play and start playing */ if ([self.audioPlayer prepareToPlay] && [self.audioPlayer play]){ NSLog(@"Started playing the recorded audio."); } else { NSLog(@"Could not play the audio."); } } else { NSLog(@"Failed to create an audio player."); } } else { NSLog(@"Stopping the audio recording failed."); } /* Here we don't need the audio recorder anymore */ self.audioRecorder = nil;}- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ if (flag){ NSLog(@"Audio player stopped correctly."); } else { NSLog(@"Audio player did not stop correctly."); } if ([player isEqual:self.audioPlayer]){ self.audioPlayer = nil; } else { /* This is not the player */ }}- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{ /* The audio session has been deactivated here */}- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags{ if (flags == AVAudioSessionInterruptionFlags_ShouldResume){ [player play]; }}#pragma mark -处理音频录制过程中的中断- (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder{ NSLog(@"Recording process is interrupted");}- (void)audioRecorderEndInterruption:(AVAudioRecorder *)recorder withFlags:(NSUInteger)flags{ if (flags == AVAudioSessionInterruptionFlags_ShouldResume){ NSLog(@"Resuming the recording..."); [recorder record]; }}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
运行结果:
2013-11-02 21:01:52.144 录制音频[1786:a0b] Successfully started to record.
2013-11-02 21:02:02.146 录制音频[1786:a0b] Successfully stopped the audio recording process.
2013-11-02 21:02:02.160 录制音频[1786:a0b] Started playing the recorded audio.
2013-11-02 21:02:12.128 录制音频[1786:a0b] Audio player stopped correctly.