2013-04-10 218 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