1. 首页
  2. 移动开发
  3. iOS
  4. 简单TOM猫学说话

简单TOM猫学说话

上传者: 2018-12-28 03:29:54上传 ZIP文件 497.46KB 热度 67次
简单实现TOM猫学说话 - (void)record { if ([self canRecord]) { NSError *error; NSMutableDictionary *_recordSetting = [[NSMutableDictionary alloc] init]; [_recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey]; [_recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; [_recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey]; [_recordSetting setValue:[NSNumber numberWithInt:AVAudioQua lityMax] forKey:AVEncoderAudioQualityKey]; //_recorderTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithString:@"recording.caf"]]]; //[_recorderTmpFile retain]; if (_recorder == NULL) { _recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:audioPath] settings:_recordSetting error:&error]; }else { [_recorder release]; _recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:audioPath] settings:_recordSetting error:&error]; } [_recorder setMeteringEnabled:YES]; [_recorder setDelegate:self]; [_recorder prepareToRecord]; // [_recorder record]; // NSLog(@"录制中~~~~~~~~~~~~~!!!!"); [_recordSetting release]; }else { NSLog(@"不能录制!!!!"); } } - (void)updateMeters:(NSTimer *)sender { //NSLog(@"~~~~~~~~~~~~~~~~~监听中!!!~~~~~"); //NSLog(@"AVG = %f;peak = %f", [[SCListener sharedListener] averagePower], [[SCListener sharedListener] peakPower]); if ([[SCListener sharedListener] averagePower] > 0.1) { if (!ISRECORDED) { if (!isRecording) { NSLog(@"1开始录制"); isRecording = YES; ISRECORDED = YES; //记录是否录音过声音 _playTimes = 0; //[self record]; [_recorder prepareToRecord]; [_recorder record]; } }else { if (!isPlaying) { if (!isRecording) { NSLog(@"2开始录制"); isRecording = YES; _playTimes = 0; [_recorder prepareToRecord]; [_recorder record]; //[self record]; } } } } if ([[SCListener sharedListener] averagePower] < 0.1) { _lowTimes++; if (_lowTimes >= 5) { //5 次 0.5s======= _lowTimes = 0; isRecording = NO; if (!isRecording & !isPlaying) { if (ISRECORDED) { if (_playTimes < 1) { _playTimes++; NSLog(@"停止录制内容"); [self stopRecord]; isPlaying = YES; } } } } } } lityMax] forKey:AVEncoderAudioQualityKey]; //_recorderTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithString:@"recording.caf"]]]; //[_recorderTmpFile retain]; if (_recorder == NULL) { _recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:audioPath] settings:_recordSetting error:&error]; }else { [_recorder release]; _recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:audioPath] settings:_recordSetting error:&error]; } [_recorder setMeteringEnabled:YES]; [_recorder setDelegate:self]; [_recorder prepareToRecord]; // [_recorder record]; // NSLog(@"录制中~~~~~~~~~~~~~!!!!"); [_recordSetting release]; }else { NSLog(@"不能录制!!!!"); } } - (void)updateMeters:(NSTimer *)sender { //NSLog(@"~~~~~~~~~~~~~~~~~监听中!!!~~~~~"); //NSLog(@"AVG = %f;peak = %f", [[SCListener sharedListener] averagePower], [[SCListener sharedListener] peakPower]); if ([[SCListener sharedListener] averagePower] > 0.1) { if (!ISRECORDED) { if (!isRecording) { NSLog(@"1开始录制"); isRecording = YES; ISRECORDED = YES; //记录是否录音过声音 _playTimes = 0; //[self record]; [_recorder prepareToRecord]; [_recorder record]; } }else { if (!isPlaying) { if (!isRecording) { NSLog(@"2开始录制"); isRecording = YES; _playTimes = 0; [_recorder prepareToRecord]; [_recorder record]; //[self record]; } } } } if ([[SCListener sharedListener] averagePower] < 0.1) { _lowTimes++; if (_lowTimes >= 5) { //5 次 0.5s======= _lowTimes = 0; isRecording = NO; if (!isRecording & !isPlaying) { if (ISRECORDED) { if (_playTimes < 1) { _playTimes++; NSLog(@"停止录制内容"); [self stopRecord]; isPlaying = YES; } } } } } }
用户评论