当前位置: 动力学知识库 > 问答 > 编程问答 >

avaudiorecorder - iOS avaudiosession not starting on certain iOS hardware

问题描述:

I have a strange situation with an AVAudioRecorder used to record audio in my iOS app. I have found some rare iPad2 devices where the audio session will not start recording. The iOS app code does record fine for all other types of iPod Touches, iPhones, and iPads as well as in the simulator. It only seems to not be able to record in a very few cases. Does anyone know why this might be happening?

Here is the coding information.


In my .h file I have the AVAudioRecorder set up...

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;

}

网友答案:

You are passing in an error in the init call:

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

but you are never reading it. I would bet that on the failed cases, you are getting an error here, causing the audioRecorder to be nil.

分享给朋友:
您可能感兴趣的文章:
随机阅读: