【发布时间】:2016-10-10 12:07:59
【问题描述】:
我正在创建一个录音框架,它可以正确编译。当我在项目中使用这个框架时,录制文件会在框架中指定的文档文件夹中创建,但它的大小保持在 4KB 并且不会增加并且文件中没有音频。我给了 30 秒的录制时间。我使用 AVFoundation 进行音频录制,如果我直接在我的项目中使用它,相同的代码可以工作,但通过自定义创建的框架调用代码不起作用。
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() 行之后添加计时器时,录音有效,但我不明白原因。
【问题讨论】:
标签: ios frameworks swift3 audio-recording