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?
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
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];
}
I call the audiosession in the viewDidLoad