我想在录制时检测声音。如果声音停止 2-3 秒,则录音应自动停止。
有什么办法吗? 我已经完成录音了:-
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"ddMMyyyyhh:mm:ss"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [dateFormatter stringFromDate:now];
dateString=[NSString stringWithFormat:@"%@.caf",dateString];
soundFilePath = [docsDir
stringByAppendingPathComponent:dateString];
NSLog(@"soundFilePath==>%@",soundFilePath);
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
[soundFilePath retain];
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error = nil;
recorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[recorder prepareToRecord];
}
[recorder record];
提前致谢
最佳答案
您应该使用 AVAudioRecorder
支持音频电平计量来跟踪音频电平,并在电平低于特定阈值时停止录制。启用计量 -
[anAVAudioRecorder setMeteringEnabled:YES];
然后你可以定期调用:
[anAVAudioRecorder updateMeters];
power = [anAVAudioRecorder averagePowerForChannel:0];
if (power > threshold && anAVAudioRecorder.recording==NO)
[anAVAudioRecorder record];
else if (power < threshold && anAVAudioRecorder.recording==YES)
[anAVAudioRecorder stop];
threshold: A floating-point representation, in decibels, of a given audio channel’s current average power. A return value of 0 dB indicates full scale, or maximum power; a return value of -160 dB indicates minimum power (that is, near silence).
If the signal provided to the audio player exceeds ±full scale, then the return value may exceed 0 (that is, it may enter the positive range).
关于ios - 自动检测AVrecorder没有声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15473970/