【问题标题】:How to name recorded file from AVAudioRecorder如何从 AVAudioRecorder 命名录制的文件
【发布时间】:2014-07-10 17:46:41
【问题描述】:

我正在制作一个 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];
}

【问题讨论】:

  • 在您设置AVAudioRecorder 以记录到文件的位置显示您的代码。
  • @rmaddy 检查我的更新
  • 我不明白这个问题。您需要做的就是将outputFileURL 更改为所需的文件。

标签: ios objective-c ios7 avaudiorecorder


【解决方案1】:

您可以简单地将文件移动到另一个路径,当您这样做时,您可以为文件指定一个新名称,这与 shell 中 mv 命令的行为完全相同。

有一个非常简单的 sn-p 让您了解如何使用移动 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
    }

【讨论】:

  • 更好的是,将正确的 URL 传递给 AVAudioRecorder 以开始。无需移动任何东西。
猜你喜欢
  • 1970-01-01
  • 2020-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
相关资源
最近更新 更多