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