2013-03-18 79 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