保存录制的 AVAudioRecorder 声音文件:现在怎么办?(iOS,Xcode 4)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9358295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving a recorded AVAudioRecorder sound file: Now what? ( iOS, Xcode 4 )
提问by Lendo92
In my App, I want the user to be able to record one sound file and play it back, and then save the sound file for later. I used this tutorialto set up the play and record interface, but this tutorial leaves out a key part: how do I permanently save the sound to the disk?
在我的应用程序中,我希望用户能够录制一个声音文件并播放它,然后保存声音文件以备后用。我使用本教程设置播放和录制界面,但是本教程遗漏了一个关键部分:如何将声音永久保存到磁盘?
Also, while you're here, how do I set a maximum time for the sound file to record? I don't want sound files exceeding 30 seconds in length.
另外,当你在这里时,我如何设置声音文件的最长时间录制?我不想要超过 30 秒的声音文件。
Thanks, hopefully I can get this all sorted out.
谢谢,希望我能解决这一切。
采纳答案by noodl_es
Voice Record Demois a great example to take a look at. It uses Cocos2D for the UI but if you just take a look at the HelloWorldScene.m class, it contains all the code you need to:
录音演示是一个很好的例子。它使用 Cocos2D 作为 UI,但如果您只看一下 HelloWorldScene.m 类,它包含您需要的所有代码:
- Create a new audio session
- Check if the mic is connected
- Start recording
- Stop recording
- Save the recorded audio file
- Play the saved voice file
- 创建新的音频会话
- 检查麦克风是否连接
- 开始录音
- 停止录音
- 保存录制的音频文件
- 播放保存的语音文件
After you init your audio session, you could use a method like the one below to save an a recording to a given filename:
初始化音频会话后,您可以使用如下所示的方法将录音保存到给定的文件名:
-(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;
}