簡體   English   中英

AVAudioRecorder顯示在IOS 5中錄制聲音的時間延遲

[英]AVAudioRecorder shows time delay for recording sound in IOS 5

我正在使用AVAudioRecorder以'wav'格式錄制聲音。 我的代碼適用於ios 4,錄制完美。 但是對於ios 5,錄制聲音的10次嘗試中有2次會出現3-4秒的時間延遲。 我還使用了:[audioRecorder prepareToRecord]功能,以便立即開始錄制。

我的代碼中是否有任何錯誤或其他錯誤...請指導我..

我的代碼是:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10];

[recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
[recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
[recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
[recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];   

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *recDir = [paths objectAtIndex:0];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audio%d.wav", recDir,nextCount]];
NSError *error = nil;
audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];
audioRecorder.delegate=self;

if(!audioRecorder){
  NSLog(@"recorder: %@ %d %@", [error domain], [error code], [[error userInfo]   description]);
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: @"Warning" message: [error localizedDescription] 
delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}


[audioRecorder prepareToRecord];
audioRecorder.meteringEnabled = YES;

BOOL audioHWAvailable = audioSession.inputIsAvailable;
if (! audioHWAvailable) {
UIAlertView *cantRecordAlert =
[[UIAlertView alloc] initWithTitle: @"Warning" message: @"Audio input hardware not available" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[cantRecordAlert show];
[cantRecordAlert release]; 
return;
}

[audioRecorder prepareToRecord];
[audioRecorder recordForDuration:(NSTimeInterval) 25];
[recordSettings release];

}

else
{
[recordButton setBackgroundImage:[UIImage imageNamed:@"record.png"] forState:UIControlStateNormal];
    self.recordBtn=NO;
    [audioRecorder stop];
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
}

問題出在AVAudioSession上。 每當我錄制視頻時,我都會創建一個錄制會話:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];

這是問題,有時會話需要時間開始,錄制延遲3-4秒。 所以,我在viewdidload中創建了會話並使其全局化並解決了問題。 感謝hotpaw2對類似鏈接的建議......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM