如何使用 AVAudioRecorder 在 iPhone 上录制音频?
既然 iPhone 3.0 SDK 已经公开,我想我可以向那些已经使用过 3.0 SDK 的人问这个问题。 我想在我的应用程序中录制音频,但我想使用 AVAudioRecorder
而不是示例 SpeakHere
所示的旧式录制方式。 iPhone 开发中心没有任何关于如何最好地做到这一点的示例,仅参考了这些类。 我是 iPhone 开发的新手,所以我正在寻找一个简单的示例来帮助我入门。
Now that iPhone 3.0 SDK is public, I think I can ask this question for those of you that have already been playing with the 3.0 SDK. I want to record audio in my application, but I want to use AVAudioRecorder
and not the older way of recording like the example SpeakHere
shows. There are not any examples of how to best do this in the iPhone Dev Center and only reference to the classes. I am a newbie at iPhone development, so I am looking for a simple sample to get me started.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
根据上面的答案,我做了一些更改,并得到了正确的输出。
第1步:在“inf.plist”下添加麦克风使用权限==>
第2步:
将录音音频文件保存到本地文档目录
播放/停止录音
这是源代码。 请看一次并使用它。
ViewController.h
ViewController.m
As per the above answers, I made some changes and I got the correct output.
Step 1: Under "inf.plist" add the Microphone usage permissions ==>
Step 2:
Save record audio file into Local Document Directory
Play/Stop recording
Here is the source code. Please have a look at once and use it.
ViewController.h
ViewController.m
这些答案都是 Objective-C 中的,这里是 Swift 中的答案。 这里唯一未包含的是允许录制所需的 pList 麦克风权限。
17 条评论中列出了这些步骤
These answers are all in Objective-C, here is an answer in Swift. The only thing not included here are the pList Microphone Permissions that are necessary to allow recording.
The steps are laid out in 17 comments
非常感谢@Massimo Cafaro和Shaybc
我能够
在 iOS 8 中实现以下任务:
1.将“AVFoundation.framework”添加到项目的
.h文件中
2.添加以下导入语句“AVFoundation/AVFoundation.h”。
3.定义“AVAudioRecorderDelegate”
4.创建一个包含“录制”、“播放”按钮及其操作方法的布局
5.定义“录音机”和“播放器”等。
以下是完整的示例代码,可能会对您有所帮助。
然后做工作
Great Thanks to @Massimo Cafaro and Shaybc
I was able achieve below tasks
in iOS 8 :
1.Add "AVFoundation.framework" to your project
in .h file
2.Add below import statement 'AVFoundation/AVFoundation.h'.
3.Define "AVAudioRecorderDelegate"
4.Create a layout with Record, Play buttons and their action methids
5.Define Recorder and Player etc.
Here is the complete example code which may help you.
Then do the job in
在以下链接中,您可以找到有关使用 AVAudioRecording 进行录音的有用信息。 在第一部分“使用音频”的这个链接中,有一个名为“使用 AVAudioRecorder 类进行录制”的锚点。 这会让你看到这个例子。
音频视频概念多媒体PG
In the following link you can find useful info about recording with AVAudioRecording. In this link in the first part "USing Audio" there is an anchor named “Recording with the AVAudioRecorder Class.” that leads you to the example.
AudioVideo Conceptual MultimediaPG
好的,我得到的答案帮助我找到了正确的方向,我非常感激。 它帮助我弄清楚如何在 iPhone 上实际录制,但我想我还应该包含一些从 iPhone 参考库中获得的有用代码:
AudioandVideoTechnologies
我使用了此代码并将其添加到 avTouch例子相当容易。 通过上面的代码示例和参考库中的示例,我能够让它很好地工作。
Ok so the answer I got helped me in the right direction and I am very thankful. It helped me figure out how to actually record on the iPhone, but I thought I would also include some helpful code I got from the iPhone Reference Library:
AudioandVideoTechnologies
I used this code and added it to the avTouch example fairly easily. With the above code sample and the sample from the reference library, I was able to get this to work pretty good.
对于下面的音频设置参考 wav 格式
:
http://objective-audio.jp/2010/09/avassetreaderavassetwriter.html
for wav format below audio setting
ref:
http://objective-audio.jp/2010/09/avassetreaderavassetwriter.html
开始
停止
START
STOP
虽然这是一个已回答的问题(而且有点旧),但我决定为其他发现很难找到良好工作(开箱即用)播放和录音示例的人发布我的完整工作代码 - 包括编码、pcm、通过扬声器播放,在此处写入文件:
AudioPlayerViewController.h:
AudioPlayerViewController.m:
希望这会对你们中的一些人有所帮助。
Although this is an answered question (and kind of old) i have decided to post my full working code for others that found it hard to find good working (out of the box) playing and recording example - including encoded, pcm, play via speaker, write to file here it is:
AudioPlayerViewController.h:
AudioPlayerViewController.m:
Hope this will help some of you guys.
我已经上传了一个示例项目。 你可以看一下。
录音机
I have uploaded a sample project. You can take a look.
VoiceRecorder
它真的很有帮助。 我遇到的唯一问题是录制后创建的声音文件的大小。 我需要减小文件大小,因此我对设置进行了一些更改。
文件大小从 360kb 减少到仅 25kb(2 秒录制)。
Its really helpful. The only problem i had was the size of sound file created after recording. I needed to reduce the file size so i did some changes in settings.
File size reduced from 360kb to just 25kb (2 seconds recording).
在过去的 2 个小时里,我一直在尝试让这段代码正常工作,尽管模拟器上没有显示错误,但设备上却出现了错误。
事实证明,至少在我的情况下,错误来自使用的目录(捆绑包):
它不可写或类似的东西...除了prepareToRecord失败的事实之外没有错误...
因此我将其替换为:
它现在就像魅力一样。
希望这对其他人有帮助。
I've been trying to get this code to work for the last 2 hours and though it showed no error on the simulator, there was one on the device.
Turns out, at least in my case that the error came from directory used (bundle) :
It was not writable or something like this... There was no error except the fact that prepareToRecord failed...
I therefore replaced it by :
It now Works like a Charm.
Hope this helps others.
事实上,根本没有例子。
这是我的工作代码。 用户按下导航栏上的按钮即可触发录制。
录音使用 CD 质量(44100 个样本)、立体声(2 通道)线性 PCM。 请注意:如果您想使用不同的格式,尤其是编码格式,请确保您完全了解如何设置 AVAudioRecorder 设置(仔细阅读音频类型文档),否则您将永远无法正确初始化它。 还有一件事。 在代码中,我没有展示如何处理计量数据,但您可以轻松弄清楚。
最后,请注意,在撰写本文时,AVAudioRecorder 方法 deleteRecording 会使您的应用程序崩溃。 这就是我通过文件管理器删除录制文件的原因。 录制完成后,我使用 KVC 将录制的音频保存为当前编辑的对象中的 NSData。
Actually, there are no examples at all.
Here is my working code. Recording is triggered by the user pressing a button on the navBar.
The recording uses cd quality (44100 samples), stereo (2 channels) linear pcm. Beware: if you want to use a different format, especially an encoded one, make sure you fully understand how to set the AVAudioRecorder settings (read carefully the audio types documentation), otherwise you will never be able to initialize it correctly. One more thing. In the code, I am not showing how to handle metering data, but you can figure it out easily.
Finally, note that the AVAudioRecorder method deleteRecording as of this writing crashes your application. This is why I am removing the recorded file through the File Manager. When recording is done, I save the recorded audio as NSData in the currently edited object using KVC.