My app can record the audio from the chat and save it in file. I recorded some music on the app screen but when I playback the audio.m4a file there is no sound coming out. The file show as "Apple MPEG-4 audio" and has 12KB size. Did I config the setting wrong? Thanks in advence.
edit: I added the stop recording function.
var assetWriter: AVAssetWriter?
var input: AVAssetWriterInput?
var channelLayout = AudioChannelLayout()
func record() {
guard let doc = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
return
}
let inputURL = docURL.appendingPathComponent("audio.m4a")
do {
try assetWriter = AVAssetWriter(outputURL: inputURL, fileType: .m4a)
} catch {
print("error: \(error)")
assetWriter = nil
return
}
guard let assetWriter = assetWriter else {
return
}
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_5_1_D
let audioSettings: [String : Any] = [
AVNumberOfChannelsKey: 6,
AVFormatIDKey: kAudioFormatMPEG4AAC_HE,
AVSampleRateKey: 44100,
AVEncoderBitRateKey: 128000,
AVChannelLayoutKey: NSData(bytes: &channelLayout, length: MemoryLayout.size(ofValue: channelLayout)),
]
input = AVAssetWriterInput(mediaType: .audio, outputSettings: settings)
guard let audioInput = input else {
print("Failed to find input.")
return
}
audioInput.expectsMediaDataInRealTime = true
if ((assetWriter.canAdd(audioInput)) != nil) {
assetWriter.add(audioInput)
}
RPScreenRecorder.shared().startCapture(handler: { (sample, bufferType, error) in
guard error == nil else {
print("Failed to capture with error: \(String(describing: error))")
return
}
if bufferType == .audioApp {
if assetWriter.status == AVAssetWriter.Status.unknown {
if ((assetWriter.startWriting()) != nil) {
assetWriter.startSession(atSourceTime: CMSampleBufferGetPresentationTimeStamp(sample))
}
}
if assetWriter.status == AVAssetWriter.Status.writing {
if audioInput.isReadyForMoreMediaData == true {
if audioInput.append(sample) == false {
}
}
}
}
})
}
func stopRecord() {
RPScreenRecorder.shared().stopCapture{ (error) in
self.audioInput.markAsFinished()
if error == nil{
self.assetWriter.finishWriting {
print("finish writing")
}
} else {
print(error as Any)
}
}
}