私はオーディオレコーディングフレームワークを作成していますが、正しくコンパイルされています。このフレームワークをプロジェクトで使用すると、記録ファイルはフレームワークで指定されたドキュメントフォルダに作成されますが、サイズは4KBのままで、ファイル内にはオーディオは増えず、オーディオもありません。私は30秒の録音時間を与えました。私はオーディオ録音にAVFoundationを使用しています。私は自分のプロジェクトで直接使用していますが、カスタム作成のフレームワークでコードを呼び出すと同じコードが機能しません。カスタムオーディオレコーダーフレームワークスウィフト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()行の後にタイマーを追加すると、録音は機能しますが、その理由はわかりません。