UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: AVAudioRecorder

Forums > Swift

Good evening,

I am writing an app to record audio. I'm having a problem I can't figure out. Basically I have a settings page that sets a value for the Audio Format this is the type of file that the user will want to create, ex: (.m4a, .wav, .mp3) etc.

Those audio format values are represented on Xcode like this:

kAudiioFormatLinearPCM = "lpcm" - the lpcm is just a 4 character code to represent it. The signature is var kAudioFormatLinearPCM: AudioFormatID { get } and the AudioFormatID signature is public typealias AudioFormatID = UInt32

Ok, I'm not sure if that all made sense. I hope so. I have setup the following enum

enum AudioFormat: UInt32 {
    case kAudioFormatLinearPCM      = 1819304813
    case kAudioFormatAppleIMA4      = 1768775988
    case kAudioFormatMPEG4AAC       = 1633772320
    case kAudioFormatMACE3          = 1296122675
    case kAudioFormatMACE6          = 1296122678
    case kAudioFormatULaw           = 1970037111
    case kAudioFormatALaw           = 1634492791
    case kAudioFormatMPEGLayer1     = 778924081
    case kAudioFormatMPEGLayer2     = 778924082
    case kAudioFormatMPEGLayer3     = 778924083
    case kAudioFormatAppleLossless  = 1634492771
}

when I use it in code to setup the settings for the recording I get an error = 2021-09-08 18:25:20.733879-0600 AudioRecorderSwiftUI[1596:73491] Write Bytes Failed

Here is how Im using it.

let recorderSettings: [String:Any] = [
            AVFormatIDKey: Int(dataController.audioFormat),
            AVSampleRateKey: AudioSampleRate.k44100.rawValue,
            AVNumberOfChannelsKey: 1,
            AVEncoderAudioQualityKey: dataController.audioQuality(qualityIndex: dataController.audioQuality).rawValue
        ]

My BarView that presents 10 bars that monitor the sound and present those levels to the user doesnt show up and the Write Bytes Failed error shows up.

Here is what recorderSettings is from above:

["AVFormatIDKey": 1819304813, "AVNumberOfChannelsKey": 1, "AVEncoderQualityKey": 32, "AVSampleRateKey": 44100]

However, if I change one line in the recorderSettings above to the following: AVFormatIDKey: Int(kAudioFormatLinearPCM) then it works just fine.

Sorry for the long write up here, but I'm trying to give all the info needed for someone to help me.

Basically, I want to be able to use the value in my DataController's UserDefaults var audioFormat which is a UInt32. I really don't understand why changing the one works

2      

Can you provide some more code so we don't have to write a bunch ourselves to test solutions?

If I try this code in a playground:

import AVFoundation

enum AudioFormat: UInt32 {
    case kAudioFormatLinearPCM      = 1819304813
    case kAudioFormatAppleIMA4      = 1768775988
    case kAudioFormatMPEG4AAC       = 1633772320
    case kAudioFormatMACE3          = 1296122675
    case kAudioFormatMACE6          = 1296122678
    case kAudioFormatULaw           = 1970037111
    case kAudioFormatALaw           = 1634492791
    case kAudioFormatMPEGLayer1     = 778924081
    case kAudioFormatMPEGLayer2     = 778924082
    case kAudioFormatMPEGLayer3     = 778924083
    case kAudioFormatAppleLossless  = 1634492771
}

let recorderSettings: [String:Any] = [
    AVFormatIDKey: Int(AudioFormat.kAudioFormatLinearPCM),
    AVSampleRateKey: 44100, //AudioSampleRate.k44100.rawValue,
    AVNumberOfChannelsKey: 1,
    AVEncoderAudioQualityKey: 32 //dataController.audioQuality(qualityIndex: dataController.audioQuality).rawValue
]

print(recorderSettings)

it won't even compile, giving me an error that Initializer 'init(_:)' requires that 'AudioFormat' conform to 'BinaryInteger' on the AVFormatIDKey line.

To make it compile, I have to change it to:

    AVFormatIDKey: Int(AudioFormat.kAudioFormatLinearPCM.rawValue),

2      

@roosterboy

Keep in mind that Im pretty new to ios development so Im not sure I can post enough code here so that you can see whats going wrong. Can I send you a zip file of my project as it is right now. If so can you give me an email address.

Note: the project uses icloud Documents and cloudkit to store the CoreData. Right now Im just trying to get this first part working. I could also commit all the code I have so far and add you as a colaborator on the github repo. Which ever is best for you, zip file or github.

2      

Ok, I figured this out, basically, I had to create an if statement that looked at what type of file they chose in the SettingsView and based on that choice set the proper kAudioFormat????.

@roosterboy, thanks for helping me. I do appreciate it very much.

Mark

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.