3
我試圖用m4a擴展名錄制語音。我用下面的代碼:在iOS 10上錄製M4A文件
try! self.audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord,
withOptions:AVAudioSessionCategoryOptions.DefaultToSpeaker)
try! self.audioSession.setActive(true)
//get documnets directory
let documentsDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let fullPath = documentsDirectory.stringByAppendingPathComponent("voiceRecord.m4a")
let url = NSURL.fileURLWithPath(fullPath)
//create AnyObject of settings
let settings: [String : AnyObject] = [
AVFormatIDKey:Int(kAudioFormatMPEG4AAC),
AVSampleRateKey:44100.0,
AVNumberOfChannelsKey:1,
AVEncoderBitRateKey:12800,
AVLinearPCMBitDepthKey:16,
AVEncoderAudioQualityKey:AVAudioQuality.Low.rawValue
]
//record
try! self.audioRecorder = AVAudioRecorder(URL: url, settings: settings)
self.audioRecorder.record()
print("kayıt başladı")
此代碼創建一個大小爲28字節的.m4a文件。如果記錄長度爲30秒或5秒,則文件大小爲28字節無關緊要。正如你可以想象的文件已損壞。
問題在哪裏?爲什麼這不能正常工作?
你應該調用'prepareToRecord()'。設置委託並檢查是否有問題('audioRecorderEncodeErrorDidOccur(_ recorder:error:)')。 – Larme
@Larme我實現了它,但它沒有返回任何你可以檢查:http://prntscr.com/e6wlcj –
你在真實設備中測試? –