2012-07-15 31 views
1

我有一個奇怪的情況,用於在我的iOS應用程序中錄製音頻的AVAudioRecorder。我發現一些罕見的iPad2設備音頻會話不會開始錄製。 iOS應用程序代碼在所有其他類型的iPod Touch,iPhone和iPad以及模擬器中都可以正常記錄。它似乎只能在極少數情況下記錄下來。有誰知道爲什麼會發生這種情況?iOS avaudiosession未開始在某些iOS硬件

這裏是編碼信息。


在我.h文件中我有AVAudioRecorder設置...

RVC.h

@interface RVC : UIViewController <AVAudioRecorderDelegate, AVAudioPlayerDelegate>{ 
    AVAudioRecorder *audioRecorder; 
} 
@property (nonatomic, retain) AVAudioRecorder *audioRecorder; 

RVC.m

@implementation RVC 
@synthesize audioRecorder; 

// Set up the file path and name, where the path is to the temporary directory 
soundFile = [NSURL fileURLWithPath:[tempDir stringByAppendingFormat:@"theSoundFile.m4a"]]; 

// Set up the settings for the recording 
soundSetting = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat: 44100.0],AVSampleRateKey, 
     [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey, 
     [NSNumber numberWithInt: 2], AVNumberOfChannelsKey, 
     [NSNumber numberWithInt: AVAudioQualityHigh], AVEncoderAudioQualityKey, nil]; 

// Make the AVAudioRecorder with the filename and the sound settings. 
self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFile settings:soundSetting error:&error]; 

// Check to make sure the iOS device has audio hardware that can make a recording 
BOOL audioHWAvailable = [[AVAudioSession sharedInstance] inputIsAvailable]; 
if (! audioHWAvailable) { 
    NSLog(@"No audio hardware is available."); 
    return; 
} 

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 

// Make sure any output sound is played through the speakers. 
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; 
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride); 

[[AVAudioSession sharedInstance] setActive:YES error:nil]; 

// If the audiorecorder is properly set up, then start recording. 
if (audioRecorder) { 
    [audioRecorder prepareToRecord]; 
    // Start the recording 
    isRecording = [audioRecorder record]; 
} 
else { 
    isRecording = FALSE; 
    NSLog(@"The audio recorder was not properly set up."); 
} 

if (isRecording) { 
    // The AVAudioSession is recording. 
} 
else { 
    // The AVAudioSession did not start so make some kind of log file. 
    NSLog(@"The recording was not started properly."); 
    // This is where THE PROBLEM SHOWS UP where it is having trouble with some versions of the iPad2, which I can not replicate in the simulators. 
    return; 
} 

回答

1

你是在init調用中傳入error

self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFile settings:soundSetting error:&error]; 

但你永遠不會讀它。我敢打賭,在失敗的案例中,你在這裏得到一個錯誤,導致audioRecorder爲nil