我正在创建一个iOS应用程序,我需要使用AVAudioRecorder录制语音,然后我想重命名该文件。现在,我可以成功录制语音,但我不知道如何为文件提供自定义名称。如何为录制的文件提供自定义名称?任何帮助表示赞赏。
在viewDidLoad
:
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
@"MyAudioMemo.m4a",
nil];
outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[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:nil];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
以及所有其他方法:
- (IBAction)recordPressed:(id)sender {
if (!recorder.recording) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
// Start recording
[recorder record];
NSLog(@"Recording...");
} else {
// Pause recording
[recorder pause];
NSLog(@"Paused...");
}
}
- (IBAction)stopPressed:(id)sender {
[recorder stop];
NSLog(@"Stopped...");
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
}
答案 0 :(得分:0)
您可以简单地将文件移动到另一个路径,当您这样做时,您可以为文件指定一个新名称,这与shell中的mv
命令完全相同。
您可以使用移动FileManager moveItemAtPath:toPath:error
方法了解如何重命名文件,这是一个非常简单的代码段:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *newPath = [docsDir stringByAppendingPathComponent:@"newname.m4a"];
// Try to move your file and the current path to newPath
if ([fileManager moveItemAtPath:currentPath toPath:newPath error:&error] != YES)
{
NSLog(@"Error: %@", [error localizedDescription]);
}
else
{
// File moved success
}