|
|
|
//
|
|
|
|
// RecordingBtn.m
|
|
|
|
// myWear
|
|
|
|
//
|
|
|
|
// Created by xTT on 2017/3/13.
|
|
|
|
// Copyright © 2017年 xTT. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "RecordingBtn.h"
|
|
|
|
#import "SVProgressHUD.h"
|
|
|
|
|
|
|
|
#import "XHVoiceRecordHUD.h"
|
|
|
|
#import "XHVoiceRecordHelper.h"
|
|
|
|
#import "CJFileUtility.h"
|
|
|
|
#import "VoiceConverter.h"
|
|
|
|
#import "XHAudioPlayerHelper.h"
|
|
|
|
|
|
|
|
@interface RecordingBtn()
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 管理录音工具对象
|
|
|
|
*/
|
|
|
|
@property (nonatomic, strong) XHVoiceRecordHelper *voiceRecordHelper;
|
|
|
|
@property (nonatomic, strong, readwrite) XHVoiceRecordHUD *voiceRecordHUD;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 判断是不是超出了录音最大时长
|
|
|
|
*/
|
|
|
|
@property (nonatomic) BOOL isMaxTimeStop;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 是否取消錄音
|
|
|
|
*/
|
|
|
|
@property (nonatomic, assign, readwrite) BOOL isCancelled;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 是否正在錄音
|
|
|
|
*/
|
|
|
|
@property (nonatomic, assign, readwrite) BOOL isRecording;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RecordingBtn
|
|
|
|
|
|
|
|
-(void)layoutSubviews
|
|
|
|
{
|
|
|
|
[super layoutSubviews];
|
|
|
|
self.layer.cornerRadius = self.frame.size.width / 2;
|
|
|
|
CGFloat midX = self.frame.size.width / 2;
|
|
|
|
CGFloat midY = self.frame.size.height/ 2 ;
|
|
|
|
self.titleLabel.center = CGPointMake(midX, midY + 20);
|
|
|
|
self.imageView.center = CGPointMake(midX, midY - 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)initCellView:(baseModel *)cModel{
|
|
|
|
// btn.frame = CGRectMake(0, 0, 80 * FIX_SCREEN, 44 * FIX_SCREEN);
|
|
|
|
self.backgroundColor = mainColor;
|
|
|
|
[self setTitle:@"录音" forState:UIControlStateNormal];
|
|
|
|
[self setImage:[myHelper getImageWithName:@"录音"]
|
|
|
|
forState:UIControlStateNormal];
|
|
|
|
|
|
|
|
[self addTarget:self action:@selector(holdDownButtonTouchDown) forControlEvents:UIControlEventTouchDown];
|
|
|
|
[self addTarget:self action:@selector(holdDownButtonTouchUpOutside) forControlEvents:UIControlEventTouchUpOutside];
|
|
|
|
[self addTarget:self action:@selector(holdDownButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
[self addTarget:self action:@selector(holdDownDragOutside) forControlEvents:UIControlEventTouchDragExit];
|
|
|
|
[self addTarget:self action:@selector(holdDownDragInside) forControlEvents:UIControlEventTouchDragEnter];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (XHVoiceRecordHUD *)voiceRecordHUD {
|
|
|
|
if (!_voiceRecordHUD) {
|
|
|
|
_voiceRecordHUD = [[XHVoiceRecordHUD alloc] initWithFrame:CGRectMake(0, 0, 140, 140)];
|
|
|
|
}
|
|
|
|
return _voiceRecordHUD;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (XHVoiceRecordHelper *)voiceRecordHelper {
|
|
|
|
if (!_voiceRecordHelper) {
|
|
|
|
_isMaxTimeStop = NO;
|
|
|
|
|
|
|
|
WEAKSELF
|
|
|
|
_voiceRecordHelper = [[XHVoiceRecordHelper alloc] init];
|
|
|
|
_voiceRecordHelper.maxTimeStopRecorderCompletion = ^{
|
|
|
|
|
|
|
|
// Unselect and unhilight the hold down button, and set isMaxTimeStop to YES.
|
|
|
|
weakSelf.selected = NO;
|
|
|
|
weakSelf.highlighted = NO;
|
|
|
|
weakSelf.isMaxTimeStop = YES;
|
|
|
|
|
|
|
|
[weakSelf finishRecorded];
|
|
|
|
};
|
|
|
|
_voiceRecordHelper.peakPowerForChannel = ^(float peakPowerForChannel) {
|
|
|
|
weakSelf.voiceRecordHUD.peakPower = peakPowerForChannel;
|
|
|
|
};
|
|
|
|
_voiceRecordHelper.maxRecordTime = kVoiceRecorderTotalTime;//20;
|
|
|
|
}
|
|
|
|
return _voiceRecordHelper;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)holdDownButtonTouchDown {
|
|
|
|
self.isCancelled = NO;
|
|
|
|
self.isRecording = NO;
|
|
|
|
|
|
|
|
WEAKSELF
|
|
|
|
[self prepareRecordWithCompletion:^BOOL{
|
|
|
|
STRONGSELF
|
|
|
|
|
|
|
|
//這邊要判斷回調回來的時候, 使用者是不是已經早就鬆開手了
|
|
|
|
if (strongSelf && !strongSelf.isCancelled) {
|
|
|
|
strongSelf.isRecording = YES;
|
|
|
|
[strongSelf startRecord];
|
|
|
|
return YES;
|
|
|
|
} else {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)holdDownButtonTouchUpOutside {
|
|
|
|
|
|
|
|
//如果已經開始錄音了, 才需要做取消的動作, 否則只要切換 isCancelled, 不讓錄音開始.
|
|
|
|
if (self.isRecording) {
|
|
|
|
WEAKSELF
|
|
|
|
[self.voiceRecordHUD cancelRecordCompled:^(BOOL fnished) {
|
|
|
|
weakSelf.voiceRecordHUD = nil;
|
|
|
|
}];
|
|
|
|
[self.voiceRecordHelper cancelledDeleteWithCompletion:^{
|
|
|
|
|
|
|
|
}];
|
|
|
|
} else {
|
|
|
|
self.isCancelled = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)holdDownButtonTouchUpInside {
|
|
|
|
|
|
|
|
//如果已經開始錄音了, 才需要做結束的動作, 否則只要切換 isCancelled, 不讓錄音開始.
|
|
|
|
if (self.isRecording) {
|
|
|
|
[self didFinishRecoingVoiceAction];
|
|
|
|
} else {
|
|
|
|
self.isCancelled = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)holdDownDragOutside {
|
|
|
|
|
|
|
|
//如果已經開始錄音了, 才需要做拖曳出去的動作, 否則只要切換 isCancelled, 不讓錄音開始.
|
|
|
|
if (self.isRecording) {
|
|
|
|
[self.voiceRecordHUD resaueRecord];
|
|
|
|
} else {
|
|
|
|
self.isCancelled = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)holdDownDragInside {
|
|
|
|
|
|
|
|
//如果已經開始錄音了, 才需要做拖曳回來的動作, 否則只要切換 isCancelled, 不讓錄音開始.
|
|
|
|
if (self.isRecording) {
|
|
|
|
[self.voiceRecordHUD pauseRecord];
|
|
|
|
} else {
|
|
|
|
self.isCancelled = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Voice Recording Helper Method
|
|
|
|
|
|
|
|
- (void)prepareRecordWithCompletion:(XHPrepareRecorderCompletion)completion {
|
|
|
|
[self.voiceRecordHelper prepareRecordingWithPath:[self getRecorderPath] prepareRecorderCompletion:completion];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)startRecord {
|
|
|
|
[self.voiceRecordHUD startRecordingHUDAtView:[self topView]];
|
|
|
|
[self.voiceRecordHelper startRecordingWithStartRecorderCompletion:^{
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)finishRecorded {
|
|
|
|
WEAKSELF
|
|
|
|
[self.voiceRecordHUD stopRecordCompled:^(BOOL fnished) {
|
|
|
|
weakSelf.voiceRecordHUD = nil;
|
|
|
|
}];
|
|
|
|
[self.voiceRecordHelper stopRecordingWithStopRecorderCompletion:^{
|
|
|
|
if ([weakSelf.voiceRecordHelper.recordDuration integerValue] < 2) {
|
|
|
|
[UICommon MessageErrorText:@"时间太短"];
|
|
|
|
}else if(weakSelf.voiceRecordHelper.recordPath){
|
|
|
|
weakSelf.block(@{@"voicePath":weakSelf.voiceRecordHelper.recordPath,
|
|
|
|
@"duration":weakSelf.voiceRecordHelper.recordDuration});
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)pauseRecord {
|
|
|
|
[self.voiceRecordHUD pauseRecord];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)resumeRecord {
|
|
|
|
[self.voiceRecordHUD resaueRecord];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)cancelRecord {
|
|
|
|
WEAKSELF
|
|
|
|
[self.voiceRecordHUD cancelRecordCompled:^(BOOL fnished) {
|
|
|
|
weakSelf.voiceRecordHUD = nil;
|
|
|
|
}];
|
|
|
|
[self.voiceRecordHelper cancelledDeleteWithCompletion:^{
|
|
|
|
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didFinishRecoingVoiceAction {
|
|
|
|
if (self.isMaxTimeStop == NO) {
|
|
|
|
[self finishRecorded];
|
|
|
|
} else {
|
|
|
|
self.isMaxTimeStop = NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//获取根视图
|
|
|
|
-(UIView*)topView{
|
|
|
|
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
|
|
|
|
return rootViewController.view;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - RecorderPath Helper Method
|
|
|
|
|
|
|
|
- (NSString *)getRecorderPath {
|
|
|
|
NSString *recorderPath = nil;
|
|
|
|
recorderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
|
|
|
|
NSDate *now = [NSDate date];
|
|
|
|
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
|
|
|
|
[dateFormatter setDateFormat:@"yyyyMMddHHmmssSSS"];
|
|
|
|
recorderPath = [recorderPath stringByAppendingPathComponent:[dateFormatter stringFromDate:now]];
|
|
|
|
recorderPath = [recorderPath stringByAppendingPathExtension:@"wav"];
|
|
|
|
return recorderPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|