audio recording sounds fuzzy

Discussion in 'iOS Programming' started by gbenna, Oct 31, 2012.

  1. gbenna macrumors member

    Joined:
    Jul 27, 2011
    #1
    I am recording audio in an app. I have it so that I start the recording with one button, then you can pause it with another button, resume with another button, and then stop it with another button. The first time I do this it is fine, however when I record a second audio it is saved but it is fuzzy sounding. What would cause this and how can I make all the recording clear sounding?

    Code:
    -(IBAction)recordAudio:(id)sender{
    
    
    NSLog(@"recordAudio");
    NSError *error;
    
    
    
    
    
    NSString *string2 = self.number.text;
    int n = [string2 intValue];
    int value = n + 1;
    NSString *string = [NSString stringWithFormat:@"%d",value];
    [self.number setText:string];
    
    
    
     /*if (isNotRecording){
        isNotRecording =NO;*/
        [audiobutton setTitle:@"Stop" forState:UIControlStateNormal];
        recStateLabel.text = @"recording";
        NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
        if(recordEncoding == ENC_IMA4)
        {
            [recordSettings setObject:[NSNumber numberWithInt: kAudioFormatAppleIMA4   ]    forKey: AVFormatIDKey];
            [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
            [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
            [recordSettings setObject:[NSNumber numberWithInt:6400] forKey:AVEncoderBitRateKey];
            [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
            [recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityMin] forKey: AVEncoderAudioQualityKey];
        }
        NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [documentPaths objectAtIndex:0];
        NSString *dataPath2 = [documentsDirectory stringByAppendingPathComponent:[[self imageNameTextField]text]];
        NSString *dataPath = [dataPath2 stringByAppendingString:@"/Audio"];
        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath  withIntermediateDirectories:NO attributes:nil error:&error];
        NSString *audioPath = [dataPath stringByAppendingPathComponent:[[self imageNameTextField]text]];
        NSString *audioPath5 = [audioPath stringByAppendingString:@"-"];
        NSString *audioPath2 = [audioPath5 stringByAppendingString:[[self number]text]];
    
        audioPath2=[audioPath2 stringByAppendingFormat:@".caf"];
        NSURL *url = [NSURL fileURLWithPath:audioPath2];
    
    
    
        recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:nil];
        [recorder setDelegate:self];
        [recorder prepareToRecord];
        [recorder record];
    
    /* }
    
    
    else {
        isNotRecording =YES;
        [audiobutton setTitle: @"Done" forState:UIControlStateNormal];
        recStateLabel.text = @"Not Recording";
        [recorder stop];
    
        NSLog(@"stopped");
    
    
       NSString *savedString1 = number.text;
       NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       NSString *documentsDirectory = [documentPaths objectAtIndex:0];
       NSString *dataPath2 = [documentsDirectory stringByAppendingPathComponent:[[self imageNameTextField]text]];
    
       NSString *documentTXTPath2 = [dataPath2 stringByAppendingPathComponent:@"counter"] ;
       documentTXTPath2 =[documentTXTPath2 stringByAppendingFormat:@".txt" ] ;
       [savedString1 writeToFile:documentTXTPath2 atomically:YES
                        encoding:NSUTF8StringEncoding error:nil];
            }*/
    }
    
    -(IBAction)pause{
    [recorder pause];
    recStateLabel.text = @"Paused";
    }
    
    -(IBAction)record{
    [recorder record];
    recStateLabel.text = @"Recording";
    }
    -(IBAction)stop{
    [recorder stop];
    recStateLabel.text = @"Done";
    
    NSString *savedString1 = number.text;
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES);
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *dataPath2 = [documentsDirectory stringByAppendingPathComponent:[[self imageNameTextField]text]];
    
    NSString *documentTXTPath2 = [dataPath2 stringByAppendingPathComponent:@"counter"] ;
    documentTXTPath2 =[documentTXTPath2 stringByAppendingFormat:@".txt" ] ;
    [savedString1 writeToFile:documentTXTPath2 atomically:YES
                     encoding:NSUTF8StringEncoding error:nil];
    }
    As you can see I have tried to have only three buttons which worked but with the same results.

    I call the audiosession in the viewDidLoad
     
  2. gbenna thread starter macrumors member

    Joined:
    Jul 27, 2011
    #2
    audio recording sounds fuzzy

    I got this figured out with some help from Jimm over at iPhone dev sdk.

    I just added recorder = nil; in the -(IBAction)stop. Works fine now.
     

Share This Page