0
我正在創建一個音頻錄製框架,它被正確編譯。當我在一個項目中使用這個框架時,錄製文件將在框架中指定的文檔文件夾中創建,但它的大小保持在4KB,並且不會增加,並且文件中沒有音頻。我給了30秒的記錄時間。我已經使用AVFoundation進行錄音,如果我直接在我的項目中使用AVFoundation,但是通過自定義創建的框架調用代碼不起作用,那麼代碼將起作用。自定義錄音機框架Swift 3.0
public func startRecording() {
do {
if (recordingSession != nil) {
return
}
recordingSession = AVAudioSession.sharedInstance()
try recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try recordingSession.setActive(true)
recordingSession.requestRecordPermission() { allowed in
DispatchQueue.main.async {
if allowed {
print("allowed")
let audioFilename = self.getDocumentsDirectory().appendingPathComponent("recording.caf")
print(audioFilename)
let settings = [
AVFormatIDKey: Int(kAudioFormatAppleIMA4),
AVSampleRateKey: 16000,
AVNumberOfChannelsKey: 1,
AVLinearPCMBitDepthKey: 16,
AVLinearPCMIsBigEndianKey: 0,
AVLinearPCMIsFloatKey: 0
]
do {
self.audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
self.audioRecorder.delegate = self
self.audioRecorder.isMeteringEnabled = true
let audioHWAvailable = self.recordingSession.isInputAvailable
if !audioHWAvailable
{
print("no audio input available")
return
}
UIApplication.shared.beginReceivingRemoteControlEvents()
self.audioRecorder.record(forDuration: 30)
if self.audioRecorder.prepareToRecord()
{
print("Recording started")
self.audioRecorder.record()
}
} catch {
self.finishRecording()
}
} else {
print("failed to record!")
}
}
}
} catch {
print("failed to record!")
}
}
我在我的項目中調用了框架類中的startRecording方法。
編輯:當我在self.audioRecorder.record()行後添加計時器時,錄製工作,但我不明白原因。