我想在录制的同时检测声音。如果声音停止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];提前感谢
发布于 2013-03-18 18:07:20
您应该使用音频级别计量的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];阈值:给定音频通道当前平均功率的浮点表示,单位为分贝。返回值为0 dB表示满量程或最大功率;返回值为-160 dB表示最小功率(即接近静默)。
如果提供给音频播放器的信号超过±满刻度,则返回值可能超过0(即可能进入正值范围)。
[apple docs]
https://stackoverflow.com/questions/15473970
复制相似问题