tags:

views:

161

answers:

1

I am trying to record Audio from my IPhone simulator and then save it into the directory and then playback. Since I cannot hear my recorded audio I am not even sure if it is being recording in the first place. Here is my code:

-(IBAction) playRecording:sender
{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *dir = [paths objectAtIndex:0]; 

    NSFileManager *filemanager = [NSFileManager defaultManager]; 

    NSArray *files = [filemanager contentsOfDirectoryAtPath:dir error:nil]; 

    NSString *file = [files objectAtIndex:0]; 

    NSString *path = [dir stringByAppendingPathComponent:file]; 

    NSURL *url = [NSURL URLWithString:path]; // url is nil 

    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 

    player.volume = 0.3; 

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

    [player play]; 


}


-(IBAction) record:sender 
{
    if(recorder.recording) 
    {
        [recorder stop]; 

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

        [recordButton setTitle:@"Record" forState:UIControlStateNormal]; 

    }

    else 
    {
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];

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

        NSString *dir = [paths objectAtIndex:0]; 
        NSString *filename = [[NSString alloc] initWithString:@"myfile.caf"]; 

        NSString *path = [dir stringByAppendingPathComponent:filename]; 

        NSMutableDictionary *settings = [[NSMutableDictionary alloc] init]; 

        [settings setValue:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey:AVFormatIDKey]; 

        [settings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 

        [settings setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey]; 

        [settings setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; 

        [settings setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; 

        [settings setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; 

        [recorder release]; 

        recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:path]
    settings:settings error:nil];

        [recorder prepareToRecord]; 
        recorder.meteringEnabled = YES; 
        [recorder record]; 

        [recordButton setTitle:@"Stop" forState:UIControlStateNormal]; 

    }
}

UPDATE 1:

The url variable in playRecording always evaluated to be nil.

A: 

The problem was that the NSURL expects a valid URL which was not supplied. Here is a way to fix the path to a valid URL which NSURL stringWithURL likes:

NSString *path = [dir stringByAppendingPathComponent:file]; 

NSString *fixedPath = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

NSURL *url = [NSURL URLWithString:fixedPath]; 

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 

player.volume = 0.3; 

[player play]; 
azamsharp