#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
{
NSURL *recordURL;
AVAudioPlayer *player;
AVAudioRecorder *recorder;
}
@end
@implementation ViewController
- (IBAction)startRecord:(UIButton *)sender {
recorder = nil;
NSFileManager *manager = [NSFileManager defaultManager];
[manager removeItemAtURL:_soundFileURL error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
NSDictionary *setting = @{AVFormatIDKey : [NSNumber numberWithInt:kAudioFormatMPEG4AAC],
AVSampleRateKey : @1000,
AVNumberOfChannelsKey : @2,
AVLinearPCMBitDepthKey : @16,
AVLinearPCMIsBigEndianKey : @NO,
AVLinearPCMIsFloatKey : @NO,
AVEncoderAudioQualityKey : [NSNumber numberWithInt:AVAudioQualityMedium]
};
recorder = [[AVAudioRecorder alloc] initWithURL:recordURL settings:setting error:nil];
[recorder prepareToRecord];
[recorder record];
}
- (IBAction)stopRecord:(UIButton *)sender {
[recorder stop];
recorder = nil;
[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategorySoloAmbient error:nil];
}
播放录音
- (IBAction)playRecord:(UIButton *)sender {
player = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:recordURL error:nil];
[player prepareToPlay];
[player play];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSString *recordString = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/sound.acc"];
recordURL = [NSURL fileURLWithPath:recordString];
}
@end