Nomad
Nomad

Reputation: 49

How to save a recorded audio in objective C (iPhone)

I am using AV audio framework to record audio files on the iPhone. Here is the current code :

NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(
                                               NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
                           stringByAppendingPathComponent:@"sound.caf"];

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

NSDictionary *recordSettings = [NSDictionary 
                                dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:AVAudioQualityMin],
                                AVEncoderAudioQualityKey,
                                [NSNumber numberWithInt:16], 
                                AVEncoderBitRateKey,
                                [NSNumber numberWithInt: 2], 
                                AVNumberOfChannelsKey,
                                [NSNumber numberWithFloat:44100.0], 
                                AVSampleRateKey,
                                nil];

NSError *error = nil;

audioRecorder = [[AVAudioRecorder alloc]
                 initWithURL:soundFileURL
                 settings:recordSettings
                 error:&error];

Now I have another function and I want to send the sound.caf file to that function something like:

NSData *soundData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:soundFilePath ofType:@"caf"]];  

But for some reason that above line seems to return a 0kb file.

Any suggestions?

Upvotes: 0

Views: 6913

Answers (2)

Augustine P A
Augustine P A

Reputation: 5068

In your code, file URL is not global. You try the code given

First of all you have to start a new audio session:

-(void) initAudioSession {

    AVAudioSession * audioSession = [AVAudioSession sharedInstance];

        [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];

        [audioSession setActive:YES error: &error];
}

-(void) recordStart {

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *docsDir = [dirPaths objectAtIndex:0];

    tempFilePath = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:[NSString stringWithFormat: @"%@.%@",audioFileName, @"caf"]]];


    NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
    [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
    [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.%@",audioFileName, @"caf"]]];


    recorder = [[ AVAudioRecorder alloc] initWithURL:tempFilePath settings:recordSetting error:&error];
    [recorder setDelegate:self];

    [recorder prepareToRecord];

    [recorder record];
}

-(void) stopRecording{
    [recorder stop];
}

-(void) playRecord{
    avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:tempFilePath error:&error];
    [avPlayer prepareToPlay];
    [avPlayer play];
}

Upvotes: 1

Helge Becker
Helge Becker

Reputation: 3253

I don't see you triggering the record method: http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioRecorder_ClassReference/Reference/Reference.html

Nor points your file open to the same file name.

Last, you cant save in the app directory. You can only save data in the Documents folder.

Upvotes: 0

Related Questions