iOS 录音功能
初始化AVAudioRecorder 对象
init(url: URL, settings: [String : Any]) throws
1. url : 音频文件保存路径
2. settings: 录制的属性
* AVFormatIDKey: 音频格式
* AVSampleRateKey: 采样频率
* AVNumberOfChannelsKey : 音频的通道数
* AVLinearPCMBitDepthKey : 设置录制音频的每个样点的位数
* AVLinearPCMIsBigEndianKey : 设置录制音频采用高位优先的记录格式
* AVLinearPCMIsFloatKey : 置采样信号的浮点数
....还有很多其它属性,具体请看文档
3. outError : 错误描述
配置和控制录音相关方法
func prepareToRecord() -> Bool
创建一个audio文件,准备录制
func record() -> Bool
启动或恢复录制。
func record(atTime time: TimeInterval) -> Bool //延迟几秒录制
NSTimeInterval recordDelay = 3.0; //must be ≥ 0
[myAudioRecorder recordAtTime:(myAudioRecorder.deviceCurrentTime + recordDelay)];
//duration 最长录制时间
func record(forDuration duration: TimeInterval) -> Bool
func pause() //暂停录制
func stop() //暂停录制
func deleteRecording() -> Bool //删除录音文件
var delegate: AVAudioRecorderDelegate? { get set }
* func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder,
successfully flag: Bool)
*func audioRecorderEncodeErrorDidOccur(AVAudioRecorder, error: Error?)
例子
let recorderSettings:[String:AnyObject] = [AVFormatIDKey : NSNumber(unsignedInt: kAudioFormatMPEG4AAC), AVSampleRateKey : 44100.0 as NSNumber, AVNumberOfChannelsKey : 2 as NSNumber, AVEncoderAudioQualityKey : AVAudioQuality.High.rawValue as NSNumber,AVEncoderBitRateKey : 320000 as NSNumber]
do {
audioRecorder = try AVAudioRecorder(URL: url, settings: recorderSettings)
audioRecorder.delegate = self
audioRecorder.prepareToRecord()
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
}catch let error as NSError{
print(error)
}