Hi guys,
I am using some code to record and save some audio. Then play it back later.
It works perfect on the Emulator, but on the device the file is size 0.
I am thinking maybe there is something I am missing.
Here is my path where the file gets saved to, and loaded from.
On the phone : /var/mobile/Applications/8CE3099A-0CA6-411F-B197-B57699076E8F/Documents/soundfile.caf
On the Emulator: /Users/johny/Library/Application Support/iPhone Simulator/4.0.2/Applications/8BE98F55-250D-49EC-B1C0-C2C4D48B4CCE/Documents/soundfile.caf
The code I use to make the path is :
NSString* fileName = [[self
documentsPath]stringByAppendingPathComponent:@"soundfile.caf"];
Method to load the audio file. This seems to work ok. At least it doest show any debug message.
-(AudioFileID)openAudioFile:(NSString*)filePath
{
NSLog(@"openAudioFile");
AudioFileID outAFID;
// use the NSURl instead of a cfurlref cuz it is easier
NSURL * afUrl = [NSURL fileURLWithPath:filePath];
OSStatus result = AudioFileOpenURL((CFURLRef)afUrl, kAudioFileReadPermission, 0,
&outAFID);
if (result != 0) NSLog(@"cannot open file: %@",filePath);
return outAFID;
}
-(UInt32)audioFileSize:(AudioFileID)fileDescriptor
{
NSLog(@"audioFileSize");
UInt64 outDataSize = 0;
UInt32 thePropSize = sizeof(UInt64);
OSStatus result = AudioFileGetProperty(fileDescriptor,
kAudioFilePropertyAudioDataByteCount, &thePropSize, &outDataSize);
if(result != 0) NSLog(@"cannot find file size");
return (UInt32)outDataSize;
}
The problem i see is the filesize is 0. Since these 2 methods work. When i print out the filesize in an NSLog it is 0.
The method where i create my AudioRecorder
- (NSError*) createAVAudioRecorder
{
NSLog(@"creatAVAudioRecorder");
[audioRecorder release];
audioRecorder = nil;
NSString *destinationString = [[self
documentsPath]stringByAppendingPathComponent:@"soundfile.caf"];
NSURL *destinationURL = [NSURL fileURLWithPath: destinationString];
NSMutableDictionary *recordSettings =[[NSMutableDictionary alloc]
initWithCapacity:10];
[recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey:
AVFormatIDKey];
float sampleRate = 44100; // 22050
[recordSettings setObject:[NSNumber numberWithFloat:sampleRate] forKey:
AVSampleRateKey];
int NumChannels = 2; //stero or mono
[recordSettings setObject:[NSNumber
numberWithInt:NumChannels]forKey:AVNumberOfChannelsKey];
int bitDepth = 16;
[recordSettings setObject:[NSNumber numberWithInt:bitDepth]
forKey:AVLinearPCMBitDepthKey];
bool bigEndian = NO;
[recordSettings setObject:[NSNumber
numberWithBool:bigEndian]forKey:AVLinearPCMIsBigEndianKey];
bool floatingSamples = NO;
[recordSettings setObject:[NSNumber
numberWithBool:floatingSamples]forKey:AVLinearPCMIsFloatKey];
NSError *recorderSetupError = nil;
audioRecorder = [[AVAudioRecorder alloc] initWithURL:destinationURL
settings:recordSettings error:&recorderSetupError];
[recordSettings release];
if (recorderSetupError)
{
NSLog(@"Record Setup Error");
UIAlertView *cantRecordAlert = [[UIAlertView alloc] initWithTitle: @"Can't
record" message: [recorderSetupError localizedDescription]delegate: nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[cantRecordAlert show];
[cantRecordAlert release];
return recorderSetupError;
}
[audioRecorder prepareToRecord];
audioRecorder.delegate = self;
return recorderSetupError;
}
I call these methods to start and stop recording
-(IBAction) startRecording
{
[self createAVAudioRecorder ];
if (! [self alertIfNoAudioInput])
return;
[audioRecorder record];
}
-(IBAction) stopRecording
{
NSLog(@"stopRecording Method");
[audioRecorder stop];
}
Many Thanks, -Code