2010-05-10 74 views
1

我正面临AVAudioRecorder的一个奇怪问题。在我的应用程序中,我需要录制音频并播放它。我正在创建我的播放器:使用AVAudioRecorder的问题

if(recorder) 
{ 
if(recorder.recording) 
[recorder stop]; 
[recorder release]; 
recorder = nil; 
} 

NSString * filePath = [NSHomeDirectory() stringByAppendingPathComponent: [NSString stringWithFormat:@"Documents/%@.caf",songTitle]]; 
     NSDictionary *recordSettings = [[NSDictionary alloc] initWithObjectsAndKeys: 
             [NSNumber numberWithFloat: 44100.0],AVSampleRateKey, 
             [NSNumber numberWithInt: kAudioFormatAppleIMA4],AVFormatIDKey, 
             [NSNumber numberWithInt: 1], AVNumberOfChannelsKey, 
             [NSNumber numberWithInt: AVAudioQualityMax],AVEncoderAudioQualityKey,nil]; 
     recorder = [[AVAudioRecorder alloc] initWithURL: [NSURL fileURLWithPath:filePath] settings: recordSettings error: nil]; 
     recorder.delegate = self; 
if ([recorder prepareToRecord] == YES){ 
       [recorder record]; 

我每次按下录制按钮都会释放并创建播放器。但问题是,AVAudiorecorder在开始记录之前需要一些时间,所以如果我连续多次按下记录按钮,我的应用程序会冻结一段时间。 当耳机连接到设备时,相同的代码工作正常,没有任何问题...录音没有延迟,即使我多次按下录音按钮,应用也不会冻结。

如果有人指导我纠正这个问题,会很有帮助。 在这方面的任何帮助将不胜感激。

Thanx提前。

回答

2

得到了解决方案。我刚刚添加了代码

AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
     NSError *err = nil; 
     [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err]; 
     if(err){ 
      NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]); 
      return; 
     } 
     err = nil; 
     [audioSession setActive:YES error:&err]; 
     if(err){ 
      NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]); 
      return; 
     } 

它现在工作正常。但是我想知道这段代码有什么区别?为什么它就像我的应用程序在连接耳机时工作正常,但没有它们就冻结了。

0

是,

[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err]; 

是解决方案。它可以让你录制和播放音频。

关于耳机的错误,这可能与物业

[[AVAudioSession sharedInstance] inputIsAvailable] 
相关