2013-03-18 74 views
0

我想在录音时检测声音。如果声音停止2-3秒,录音应自动停止。自动检测到没有声音AVrecorder

有什么办法吗? 我做了记录: -

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]; 

在此先感谢

回答

1

您shoudl用于音频电平表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]

+0

感谢您的快速reply.But什么呢门槛意味着在这里 – 2013-03-18 10:17:24