2013-04-10 273 views

回答

0

它可能與iOS 6.我有問題在iOS 5.x自己做。沒有關於5.x之前版本的信息。

爲了做到這一點,在AudioSession類必須被設置爲AVAudioSessionCategoryPlayAndRecord

NSError *error = nil; 
NSLog(@"Setting category for AudioSession to AVAudioSessionCategoryPlayAndRecord"); 
BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]; 
if (!success) { 
    NSLog(@"Error setting category for AudioSession:\n\terror: %@", error.localizedDescription); 
} 

的AVAudioRecorder是設置這樣的:

NSString *fileToRecordPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"record.m4a"]; 
NSURL *fileToRecordURL = [NSURL fileURLWithPath:fileToRecordPath]; 
NSDictionary *settings = @{AVFormatIDKey: [NSNumber numberWithInt:kAudioFormatMPEG4AAC], 
          AVSampleRateKey: [NSNumber numberWithFloat:44100.0], 
          AVNumberOfChannelsKey: [NSNumber numberWithInt:1]}; 
NSError *error = nil; 

AVAudioRecorder *audioRecorder = [[AVAudioRecorder alloc] initWithURL:self.fileURL settings:settings error:&error]; 
if (!audioRecorder) { 
    NSLog(@"Error initializing AVAudioRecorder:\n\terror: %@", error.localizedDescription); 
} 
audioRecorder.delegate = self; 
[audioRecorder prepareToRecord]; 

當播放其他音頻文件(格式相同的,m4a)在AVAudioPlayer中一切正常在iOS 6中。在iOS 5中,行爲很奇怪。播放音頻或音頻播放暫停時,無法開始錄製。當開始首先錄製時,雖然可以同時播放(另一個文件)。

經過一番搜索後,我無法找到正確設置它的方法。雖然我不能相信,但這是iOS中的一個錯誤,但是缺少一個設置。

所以這在iOS 6,但幫助了iOS 5的問題,認識了很多; O)

+0

你在哪裏播放音頻代碼? – 2014-01-01 06:22:21

+0

我不知道。這是AVAudioSessionCategory和AVAudioRecorder的設置。按照記錄使用AVAudioPlayer。對我來說,iOS 5中的不尋常行爲如下所述。 – 2014-01-20 12:44:15