修复bug
This commit is contained in:
@@ -10,17 +10,16 @@
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface RecordVoiceManager : NSObject
|
||||
/// 声音录制对象
|
||||
@property (nonatomic, strong) AVAudioRecorder *audioRecorder;
|
||||
/// 本地录制的音频地址
|
||||
@property (nonatomic, copy) NSString *totalFilePath;
|
||||
/// 音频文件名
|
||||
@property (nonatomic, copy) NSString *fileName;
|
||||
///播放器4
|
||||
|
||||
@property (nonatomic, strong) AVAudioRecorder *recorder;
|
||||
|
||||
@property (nonatomic, copy) NSString *path;
|
||||
|
||||
@property (nonatomic, copy) NSString *file;
|
||||
|
||||
@property (nonatomic, strong) AVAudioPlayer *player;
|
||||
/// 是否正在播放声音
|
||||
|
||||
@property (nonatomic, assign) BOOL isPlayVoice;
|
||||
/// 初始化录音对象
|
||||
- (void)initWithRecord;
|
||||
-(void)stopRecord;
|
||||
- (void)playFilePathVoice;
|
||||
|
@@ -9,8 +9,8 @@
|
||||
|
||||
@implementation RecordVoiceManager
|
||||
-(void)stopRecord{
|
||||
[self.audioRecorder stop];
|
||||
self.audioRecorder = nil;
|
||||
[self.recorder stop];
|
||||
self.recorder = nil;
|
||||
}
|
||||
/// 初始化录音对象
|
||||
- (void)initWithRecord {
|
||||
@@ -18,47 +18,38 @@
|
||||
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
|
||||
//录音设置
|
||||
NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
|
||||
//录音格式
|
||||
[recordSettings setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey: AVFormatIDKey];
|
||||
//采样率
|
||||
[recordSettings setValue :[NSNumber numberWithFloat:16000.0] forKey: AVSampleRateKey];
|
||||
//通道数
|
||||
[recordSettings setValue :[NSNumber numberWithInt:2] forKey: AVNumberOfChannelsKey];
|
||||
//线性采样位数
|
||||
[recordSettings setValue :[NSNumber numberWithInt:16] forKey: AVLinearPCMBitDepthKey];
|
||||
//音频质量,采样质量
|
||||
[recordSettings setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];
|
||||
NSError *error = nil;
|
||||
|
||||
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
|
||||
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
|
||||
NSString *date = [dateFormat stringFromDate:[NSDate date]];
|
||||
self.fileName = [NSString stringWithFormat:@"voiceShow_%@.wav", date];
|
||||
self.file = [NSString stringWithFormat:@"voiceShow_%@.wav", date];
|
||||
NSFileManager *fileMgr = [[NSFileManager alloc] init];
|
||||
//创建文件管理器
|
||||
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"voiceShow"];
|
||||
self.totalFilePath = [filePath stringByAppendingPathComponent:self.fileName];
|
||||
self.path = [filePath stringByAppendingPathComponent:self.file];
|
||||
if (![fileMgr fileExistsAtPath:filePath]) {
|
||||
//如果不存在,则说明是第一次运行这个程序,那么建立这个文件夹
|
||||
[fileMgr createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
|
||||
}
|
||||
|
||||
if (![fileMgr fileExistsAtPath:filePath]) { return; }
|
||||
|
||||
NSURL *fileUrl = [NSURL fileURLWithPath:self.totalFilePath];
|
||||
self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:fileUrl settings:recordSettings error:&error];
|
||||
self.audioRecorder.meteringEnabled = YES;
|
||||
[self.audioRecorder updateMeters];
|
||||
NSLog(@"%f",[self.audioRecorder peakPowerForChannel:0]);
|
||||
if(!self.audioRecorder) {
|
||||
// [MewHUDTool showErrorWithMessage:@"录制失败,请重试"];
|
||||
NSURL *fileUrl = [NSURL fileURLWithPath:self.path];
|
||||
self.recorder = [[AVAudioRecorder alloc] initWithURL:fileUrl settings:recordSettings error:&error];
|
||||
self.recorder.meteringEnabled = YES;
|
||||
[self.recorder updateMeters];
|
||||
NSLog(@"%f",[self.recorder peakPowerForChannel:0]);
|
||||
if(!self.recorder) {
|
||||
return;
|
||||
}
|
||||
if(![self.audioRecorder prepareToRecord]) {
|
||||
// [MewHUDTool showErrorWithMessage:@"录制失败,请重试"];
|
||||
if(![self.recorder prepareToRecord]) {
|
||||
return;
|
||||
}
|
||||
BOOL status = [self.audioRecorder record];
|
||||
BOOL status = [self.recorder record];
|
||||
NSLog(@"%d",status);
|
||||
}
|
||||
- (void)playFilePathVoice {
|
||||
@@ -70,10 +61,10 @@
|
||||
}
|
||||
|
||||
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
|
||||
BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:self.totalFilePath];
|
||||
BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:self.path];
|
||||
|
||||
NSError *AVerror = NULL;
|
||||
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:self.totalFilePath] error:&AVerror];
|
||||
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:self.path] error:&AVerror];
|
||||
self.player.volume = 1;
|
||||
self.player.numberOfLoops = -1;
|
||||
|
||||
|
Reference in New Issue
Block a user