Playing recorded audio results in ACMP4AACBaseDecoder failing

97 views Asked by At

I'm playing around with a really simple audio use case. I'm recording via AVAudioRecorder and playing the recorded audio via AVAudioPlayer. The way I configured it is to set up the AVAudioRecorder once when the view controller is presented and record over the same file whenever record is initiated. Playing will play the url used by the recorder. The first recording and play back work fine. However if I try to record and play again I get the following:

2022-12-30 10:39:09.874066-0800 [40936:5138438]  Error deserializing gain control data
2022-12-30 10:39:09.874246-0800 [40936:5138438]  Error deserializing right channel stream
2022-12-30 10:39:09.874443-0800 [40936:5138438]  Error in deserializing element
2022-12-30 10:39:09.874545-0800 [40936:5138438]  Error deserializing packet
2022-12-30 10:39:09.874675-0800 [40936:5138438] [ac]   ACMP4AACBaseDecoder.cpp:1438  (0x127850040) Error decoding packet 1: err = -1, packet length: 198
2022-12-30 10:39:09.874781-0800 [40936:5138438] [ac]   ACMP4AACBaseDecoder.cpp:1447  '21 4E E5 44 87 E2 FC 62 0A 9B 64 42 80 D5 4E 01 17 D0 9D 47 CF FA E0 91 D2 5C FD 28 8B 3E 04 3A FD 71 F0 43 6D 20 48 52 EB 6C 73 7D 30 98 41 CC 12 F9 27 C3 10 60 4F 67 41 F6 4F 59 F1 A6 45 A4 B4 87 6A 4F 34 9C 26 CF 03 9A 9C 8D 20 14 6E 72 E6 C5 08 A6 BC D7 3B DE 3D 7F 61 1A 23 38 EC 95 1E 71 C8 1A D4 F1 B0 6E ED 9C 53 D3 FD 0E E1 5B AA CD 83 8F D3 0D 40 D7 D1 94 B4 F5 A8 1A 13 4D CE E2 37 69 14 FA E9 01 A2 50 2C AE 9C 75 49 04 06 8B 6F 01 23 20 0D 3E F1 5B 6F 47 D3 F3 F3 8A 06 E9 C5 AB 34 84 28 21 C7 4D DA 47 BE 9E 1A D5 6A B8 74 B2 EA 21 1D 51 0B 2D CE 1C 3F 01 F8 0F C0 7E 10 EB F5 C7'
2022-12-30 10:39:09.876272-0800 [40936:5138438]  Too few bits left in input buffer

I'm not very knowledgable about how audio works beneath the hood but I figured this is pretty basic audio stuff.

How I set up the recorder (only done once when VC is loaded):

func setupAudioRecorder() {
    // Set the audio file
    let audioFileURL = getFileUrl()
    
    // Setup audio session
    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(.playAndRecord, mode: .default, options: [])
    } catch _ {
    }
    
    // Define the recorder setting
    let recorderSetting = [AVFormatIDKey: NSNumber(value: kAudioFormatMPEG4AAC as UInt32),
                           AVSampleRateKey: 44100.0,
                           AVNumberOfChannelsKey: 2 ]
    
    audioRecorder = try? AVAudioRecorder(url: audioFileURL, settings: recorderSetting)
    audioRecorder?.delegate = self
    audioRecorder?.isMeteringEnabled = true
    audioRecorder?.prepareToRecord()
}

func getDocumentsDirectory() -> URL
{
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

func getFileUrl() -> URL
{
    let timeInterval = Int(NSDate().timeIntervalSince1970*1000)
    let audioFileName = "version_" + String(timeInterval) + ".m4a"
    let filePath = getDocumentsDirectory().appendingPathComponent(audioFileName)
    return filePath
}

Start recording:

func startRecording() {
    if let recorder = audioRecorder {
        if !recorder.isRecording {
            let audioSession = AVAudioSession.sharedInstance()
            
            do {
                try audioSession.setActive(true)
            } catch _ {
            }
            
            // Start recording
            recorder.record()
            print("RECORD " + recorder.url.absoluteString)
        }
    }
}

How I set up the player (called every time user hits play):

func setUpPlayer() {
    guard let _ = self.audioPlayer  else {
        self.audioPlayer = try? AVAudioPlayer(contentsOf: self.audioRecorder!.url)
        self.audioPlayer?.delegate = self
        setAudioPlayerToUseSpeaker()
        return
    }
}

Toggle player:

func toggleAudioPlayer() {
    if let audioPlayer = self.audioPlayer {
        if (audioPlayer.isPlaying) {
            audioPlayer.stop()
        } else {
            audioPlayer.play()
        }
    }
}
0

There are 0 answers