录音
#import "ViewController.h"
#import "CDMyUtil.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController () {
AVAudioRecorder *myRecorder;
NSDictionary *dict;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
dict = @{
AVEncoderAudioQualityKey:[NSNumber numberWithInt:AVAudioQualityLow],
AVEncoderBitRateKey:[NSNumber numberWithInt:16],
AVNumberOfChannelsKey:[NSNumber numberWithInt:2],
AVSampleRateKey:[NSNumber numberWithInt:44100]
};
NSLog(@"%@", NSHomeDirectory());
}
- (IBAction)recordButtonClicked:(UIButton *)sender {
if (!myRecorder) {
NSString *filePath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@.caf", [CDMyUtil timeStringWithFormat:@"yyyyMMddHHmmssSSS"]]];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
myRecorder = [[AVAudioRecorder alloc] initWithURL:fileUrl settings:dict error:nil];
}
if (myRecorder.isRecording) {
[myRecorder stop];
[sender setTitle:@"录音" forState:UIControlStateNormal];
myRecorder = nil;
}
else {
[myRecorder prepareToRecord];
[myRecorder record];
[sender setTitle:@"结束" forState:UIControlStateNormal];
}
}
@end