【问题标题】:Saving a recorded AVAudioRecorder sound file: Now what? ( iOS, Xcode 4 )保存录制的 AVAudioRecorder 声音文件:现在怎么办? (iOS,Xcode 4)
【发布时间】:2012-02-20 08:51:21
【问题描述】:

在我的应用程序中,我希望用户能够录制一个声音文件并播放它,然后保存声音文件以备后用。我使用this tutorial设置播放和录音界面,但是本教程遗漏了一个关键部分:如何将声音永久保存到磁盘?

另外,当您在这里时,如何设置声音文件录制的最长时间?我不想要长度超过 30 秒的声音文件。

谢谢,希望我能解决这一切。

【问题讨论】:

  • -1'ed for "Crappy tutorial link" - 它缺少你想要的东西这一事实并没有让它变得糟糕,特别是因为它已经做了多少工作是显而易见的。 (我与该教程无关)。

标签: objective-c xcode file audio avaudiorecorder


【解决方案1】:

Voice Record Demo 是一个很好的例子。它使用 Cocos2D 作为 UI,但如果您只看一下 HelloWorldScene.m 类,它包含您需要的所有代码:

  1. 创建一个新的音频会话
  2. 检查麦克风是否已连接
  3. 开始录制
  4. 停止录制
  5. 保存录制的音频文件
  6. 播放保存的语音文件

启动音频会话后,您可以使用如下方法将录音保存到给定文件名:

-(void) saveAudioFileNamed:(NSString *)filename {

destinationString = [[self documentsPath] stringByAppendingPathComponent:filename];
NSLog(@"%@", destinationString);
NSURL *destinationURL = [NSURL fileURLWithPath: destinationString];

NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
                          [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
                          [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                          [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
                          nil];

NSError *error;

recorder = [[AVAudioRecorder alloc] initWithURL:destinationURL settings:settings error:&error];
recorder.delegate = self;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    相关资源
    最近更新 更多