1

I am new to ios development and facing problem in creating an app. My app can record audio and save and play it back again. I have used AVAudiorecorder and AVAudioplayer. As soon as the recording is stopped, it asks the user to give the name (in textfield) to the file to save and then, the list of all saved files should appear and can be played on the next screen. (same as the italk recorder app) I want to save the recorded file in documents directory and want a database that will contain metadata of all audio files. Database should have id, date, title, path.

This is what i have done :

if (player.playing) {
        [player stop];
    }

    if (!recorder.recording) {

        [self updatefilecounter];
        AVAudioSession *session = [AVAudioSession sharedInstance];
        [session setActive:YES error:nil];

        // Set the audio file
        //filename = @"nnnn";
        filename = @"Music/nnnn";
        filename = [filename stringByAppendingString:@(filecounter).stringValue];
        filename = [filename stringByAppendingString:@".m4a"];

NSArray *pathComponents = [NSArray arrayWithObjects:
                          [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],filename,nil];

 NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

        // Setup audio session
       [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

        // Define the recorder setting
        NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
        [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
        [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
        [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

        // Initiate and prepare the recorder
        recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
        recorder.delegate = self;
        recorder.meteringEnabled = YES;

        NSLog(@"%@", outputFileURL);

        NSLog(@"%@", filename);
        [recorder record];

    } else {

        // Stop recording
        [recorder stop];
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        [audioSession setActive:NO error:nil];

SaveAudioVC *vc = [self.storyboardinstantiateViewControllerWithIdentifier:@"SaveAudioVC"];

        [self.navigationController pushViewController:vc animated:YES];
    }
}

File gets saved as nnnn.1, nnnn.2 and so on, but when i stop the app and run again it overwrites the last files and i want the the user to give the name of his/her choice to save the file and display in last screen with the given name to the recorded file. I have referred this link : Record audio and save permanently in iOS

But my other requirements are not matching so plz guide me to save the recorded file in documents directory, give the name to the file as per user wish in the text field and display all the saved files and create a database as mentioned above.

Community
  • 1
  • 1
nnnnn
  • 21
  • 3

1 Answers1

1

i think you should give path string like [@"Music" stringByAppendingByPathComponents:@"nnnn"] instead of filename = @"Music/nnnn";

i hope it will give you some idea.
when i starts audio recording i give it temporary name like temp.m4a
than when i stops recording i use bellow method to replace temporary name with new name.

/*
 give name and replace it with defaultName'temp.m4a'
 */
- (void)convertNewName:(NSString*)newname{
    NSString* folder=[documentDirectory stringByAppendingPathComponent:@"VoiceDB"];
    NSString* defaultfile=[folder stringByAppendingPathComponent:@"temp.m4a"];
    newname=[NSString stringWithFormat:@"%@.m4a",newname];
    NSString *newPath = [[defaultfile stringByDeletingLastPathComponent] stringByAppendingPathComponent:newname];
    [[NSFileManager defaultManager] moveItemAtPath:defaultfile toPath:newPath error:nil];
}
Vatsal Shukla
  • 1,274
  • 12
  • 25