From 3e8437ae559487362fae3525beb79c534c213a51 Mon Sep 17 00:00:00 2001 From: 单军华 Date: Thu, 12 Jul 2018 13:44:34 +0800 Subject: [PATCH] bug修复和功能优化 --- screendisplay/Pods/EaseUI/EaseUI/EMUIKit/3rdparty/DeviceHelper/internal/EMAudioRecorderUtil.m | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 175 insertions(+), 0 deletions(-) diff --git a/screendisplay/Pods/EaseUI/EaseUI/EMUIKit/3rdparty/DeviceHelper/internal/EMAudioRecorderUtil.m b/screendisplay/Pods/EaseUI/EaseUI/EMUIKit/3rdparty/DeviceHelper/internal/EMAudioRecorderUtil.m new file mode 100755 index 0000000..00f23a9 --- /dev/null +++ b/screendisplay/Pods/EaseUI/EaseUI/EMUIKit/3rdparty/DeviceHelper/internal/EMAudioRecorderUtil.m @@ -0,0 +1,175 @@ +/************************************************************ + * * Hyphenate CONFIDENTIAL + * __________________ + * Copyright (C) 2016 Hyphenate Inc. All rights reserved. + * + * NOTICE: All information contained herein is, and remains + * the property of Hyphenate Inc. + * Dissemination of this information or reproduction of this material + * is strictly forbidden unless prior written permission is obtained + * from Hyphenate Inc. + */ +#import "EMAudioRecorderUtil.h" +#import "EaseLocalDefine.h" + +static EMAudioRecorderUtil *audioRecorderUtil = nil; + +@interface EMAudioRecorderUtil () <AVAudioRecorderDelegate> { + NSDate *_startDate; + NSDate *_endDate; + + void (^recordFinish)(NSString *recordPath); +} +@property (nonatomic, strong) AVAudioRecorder *recorder; +@property (nonatomic, strong) NSDictionary *recordSetting; + +@end + +@implementation EMAudioRecorderUtil + +#pragma mark - Public + ++(BOOL)isRecording{ + return [[EMAudioRecorderUtil sharedInstance] isRecording]; +} + +// Start recording ++ (void)asyncStartRecordingWithPreparePath:(NSString *)aFilePath + completion:(void(^)(NSError *error))completion{ + [[EMAudioRecorderUtil sharedInstance] asyncStartRecordingWithPreparePath:aFilePath + completion:completion]; +} + +// Stop recording ++(void)asyncStopRecordingWithCompletion:(void(^)(NSString *recordPath))completion{ + [[EMAudioRecorderUtil sharedInstance] asyncStopRecordingWithCompletion:completion]; +} + +// Cancel recording ++(void)cancelCurrentRecording{ + [[EMAudioRecorderUtil sharedInstance] cancelCurrentRecording]; +} + ++(AVAudioRecorder *)recorder{ + return [EMAudioRecorderUtil sharedInstance].recorder; +} + +#pragma mark - getter +- (NSDictionary *)recordSetting +{ + if (!_recordSetting) { + _recordSetting = [[NSDictionary alloc] initWithObjectsAndKeys: + [NSNumber numberWithFloat: 8000.0],AVSampleRateKey, //��������� + [NSNumber numberWithInt: kAudioFormatLinearPCM],AVFormatIDKey, + [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,//������������ ������ 16 + [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,//��������������� + nil]; + } + + return _recordSetting; +} + +#pragma mark - Private ++(EMAudioRecorderUtil *)sharedInstance{ + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + audioRecorderUtil = [[self alloc] init]; + }); + + return audioRecorderUtil; +} + +-(instancetype)init{ + if (self = [super init]) { + + } + + return self; +} + +-(void)dealloc{ + if (_recorder) { + _recorder.delegate = nil; + [_recorder stop]; + [_recorder deleteRecording]; + _recorder = nil; + } + recordFinish = nil; +} + +-(BOOL)isRecording{ + return !!_recorder; +} + +// Start recording���save the audio file to the path +- (void)asyncStartRecordingWithPreparePath:(NSString *)aFilePath + completion:(void(^)(NSError *error))completion +{ + NSError *error = nil; + NSString *wavFilePath = [[aFilePath stringByDeletingPathExtension] + stringByAppendingPathExtension:@"wav"]; + NSURL *wavUrl = [[NSURL alloc] initFileURLWithPath:wavFilePath]; + _recorder = [[AVAudioRecorder alloc] initWithURL:wavUrl + settings:self.recordSetting + error:&error]; + if(!_recorder || error) + { + _recorder = nil; + if (completion) { + error = [NSError errorWithDomain:NSEaseLocalizedString(@"error.initRecorderFail", @"Failed to initialize AVAudioRecorder") + code:-1 + userInfo:nil]; + completion(error); + } + return ; + } + _startDate = [NSDate date]; + _recorder.meteringEnabled = YES; + _recorder.delegate = self; + + [_recorder record]; + if (completion) { + completion(error); + } +} + +// Stop recording +-(void)asyncStopRecordingWithCompletion:(void(^)(NSString *recordPath))completion{ + recordFinish = completion; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + [self->_recorder stop]; + }); +} + +// Cancel recording +- (void)cancelCurrentRecording +{ + _recorder.delegate = nil; + if (_recorder.recording) { + [_recorder stop]; + } + _recorder = nil; + recordFinish = nil; +} + + +#pragma mark - AVAudioRecorderDelegate +- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder + successfully:(BOOL)flag +{ + NSString *recordPath = [[_recorder url] path]; + if (recordFinish) { + if (!flag) { + recordPath = nil; + } + recordFinish(recordPath); + } + _recorder = nil; + recordFinish = nil; +} + +- (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder + error:(NSError *)error{ + NSLog(@"audioRecorderEncodeErrorDidOccur"); +} +@end -- Gitblit v1.8.0