录音简单使用

来源:互联网 发布:java高级工程师面试题 编辑:程序博客网 时间:2024/05/13 13:12
#import "ViewController.h"#import <AVFoundation/AVFoundation.h>@interface ViewController ()<AVAudioPlayerDelegate>@property (nonatomic,strong) NSURL *recordedFile;@property (nonatomic,strong) AVAudioRecorder *recorder;@property (nonatomic,strong) AVAudioPlayer *play;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];            // 临近状态检测        bool proximityState = [UIDevice currentDevice].proximityState;    NSLog(@"------->%d",proximityState);                UIDevice *device = [UIDevice currentDevice];    device.proximityMonitoringEnabled = YES; // 允许临近检测    if (device.proximityMonitoringEnabled == YES ) {                // 临近消息触发        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(proximityChanged:) name:UIDeviceProximityStateDidChangeNotification object:device];    }        // 设置好文件的存储位置    NSFileManager *manager = [NSFileManager defaultManager];            NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);        NSString *path = [pathArray lastObject];    path = [path stringByAppendingPathComponent:@"RecordedFile"];        bool existsFlag = [manager fileExistsAtPath:path];    if (!existsFlag) {                bool isSuccess = [manager createFileAtPath:path contents:nil attributes:nil];                        if (isSuccess) {            NSLog(@"创建成功");        }else{            NSLog(@"创建失败");        }    }        _recordedFile = [NSURL fileURLWithPath:path];    AVAudioSession *session = [AVAudioSession sharedInstance];    NSError *error;        // 开启录音和播放功能    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];        if (session == nil) {        NSLog(@"session is error ---->%@",error.description);    }else{        [session setActive:true error:nil];    }}- (void)proximityChanged:(NSNotification *)notification{    // 如果此时手机靠近面部, 声音从听筒出来, 屏幕变暗    if ([UIDevice currentDevice].proximityState == YES) { // 接近耳朵                [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];            }else{               // 没有黑屏的话, 就直接开扬声器。        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];    }}- (IBAction)recordAction:(id)sender {        // 先设置播放和录音状态    [[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];        // 初始化录音    _recorder = [[AVAudioRecorder alloc]initWithURL:_recordedFile settings:nil error:nil];        // 准备录音    [_recorder prepareToRecord];        // 开始录音    [_recorder record];        NSLog(@"开始录音.....");}- (IBAction)stopAction:(id)sender {        [_recorder stop];        _recorder = nil;        NSLog(@"结束录音......");                NSError *error;    _play = [[AVAudioPlayer alloc]initWithContentsOfURL:_recordedFile error:&error];    if (_play == nil) {        NSLog(@"error is --->%@",error.description);    }    _play.delegate = self;    //设置从扬声器播放    [[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayback error:nil];}- (IBAction)playerAction:(id)sender {            if ([_play isPlaying]) {                [_play pause];            }else{               [_play play];        }            }- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{        NSLog(@"播放完成");}@end

0 0