将录制的音频保存在文档目录中,并创建包含所有音频文件的数据库

时间:2015-01-21 09:50:34

标签: ios objective-c audio

我是ios开发的新手,在创建应用时面临问题。 我的应用程序可以录制音频并保存并再次播放。 我使用过AVAudiorecorder和AVAudioplayer。 一旦录制停止,它会要求用户将名称(在文本字段中)提供给要保存的文件,然后,应显示所有已保存文件的列表,并可在下一个屏幕上播放。 (与italk录音机应用程序相同) 我想将录制的文件保存在文档目录中,并希望有一个包含所有音频文件元数据的数据库。数据库应该有id,date,title,path。

这就是我所做的:

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];
    }
}

文件保存为nnnn.1,nnnn.2等等,但当我停止应用程序并再次运行时,它会覆盖最后的文件,我希望用户提供他/她选择的名称以保存文件并显示在最后一个屏幕中,并使用给定名称记录文件。 我已经提到了这个链接:Record audio and save permanently in iOS

但是我的其他要求不匹配,所以plz引导我将记录的文件保存在文档目录中,根据用户的意愿在文本字段中给出文件的名称,并显示所有保存的文件并创建如上所述的数据库

1 个答案:

答案 0 :(得分:0)

我认为你应该提供类似[@"Music" stringByAppendingByPathComponents:@"nnnn"]而不是filename = @"Music/nnnn";的路径字符串
我希望它会给你一些想法。
当我开始录音我给它一个临时名称,如 temp.m4a
,当我停止录制时,我使用波纹管方法用新名称替换临时名称。

/*
 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];
}
相关问题