iOS 錄音功能
初始化AVAudioRecorder 對(duì)象
- 初始化一個(gè)AVAudioRecorder 對(duì)象
init(url: URL, settings: [String : Any]) throws
1. url : 音頻文件保存路徑
2. settings: 錄制的屬性
* AVFormatIDKey: 音頻格式
* AVSampleRateKey: 采樣頻率
* AVNumberOfChannelsKey : 音頻的通道數(shù)
* AVLinearPCMBitDepthKey : 設(shè)置錄制音頻的每個(gè)樣點(diǎn)的位數(shù)
* AVLinearPCMIsBigEndianKey : 設(shè)置錄制音頻采用高位優(yōu)先的記錄格式
* AVLinearPCMIsFloatKey : 置采樣信號(hào)的浮點(diǎn)數(shù)
....還有很多其它屬性对嚼,具體請(qǐng)看文檔
3. outError : 錯(cuò)誤描述
配置和控制錄音相關(guān)方法
func prepareToRecord() -> Bool
創(chuàng)建一個(gè)audio文件斗这,準(zhǔn)備錄制
func record() -> Bool
啟動(dòng)或恢復(fù)錄制佑淀。
func record(atTime time: TimeInterval) -> Bool //延遲幾秒錄制
NSTimeInterval recordDelay = 3.0; //must be ≥ 0
[myAudioRecorder recordAtTime:(myAudioRecorder.deviceCurrentTime + recordDelay)];
//duration 最長(zhǎng)錄制時(shí)間
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)
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者