[ad_1]
Im engaged on a mission that consists of recording a watermelon thump and predict its sweetness by the sound it produces. Im utilizing AVAudioRecorder to report and save the audio. however when I attempt to open and browse the file it provides me an error (This error solely happens after I check the app on my precise iPhone Xr however not after I use the xcode simulator) the error code is: “Basis._GenericObjCError error 0” .
Right here is the recording operate and fetching audios operate:
non-public func startRecording() {
do {
if self.report{
// recording already began
recorder.cease()
self.report.toggle()
// updating information for each recording
self.getAudios()
return
}
// report audio
// retailer audio in doc listing
// var highPassFilter = AVAudioUnitEQFilterType.highPass
// var lowPassFilter = AVAudioUnitEQFilterType.lowPass
let fileURL: URL = {
let src = "thump.wav"
return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent(src)
}()
// let fileURL : URL = {
// return Bundle.major.url(forResource: "thump", withExtension: "wav")
// }()!
let recordSettings: [String : Any] = [AVFormatIDKey: Int(kAudioFormatLinearPCM),
AVSampleRateKey: 44100.0,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue ]
self.recorder = attempt AVAudioRecorder(url: fileURL, settings: recordSettings)
print(fileURL)
self.recorder.report()
print("Recording began")
self.report.toggle()
} catch {
print(error.localizedDescription)
}
}
func getAudios() {
do {
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
// Fetch all information from doc listing
let consequence = attempt FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, choices: .producesRelativePathURLs)
self.audios.removeAll()
for i in consequence {
self.audios.append(i)
}
}
catch {
print(error.localizedDescription)
}
}
And that is the operate the place I get the error within the file.learn(into:buf):
func readAudioIntoFloats(fname: String, ext: String) -> [Float] {
let interleaved: Bool = false
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileName = url.appendingPathComponent("(fname).(ext)")
// let fileName = Bundle.major.url(forResource: "thump", withExtension: "wav")
print(fileName)
let file = attempt! AVAudioFile(forReading: fileName)
let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: file.fileFormat.sampleRate, channels: file.fileFormat.channelCount, interleaved: interleaved)!
let buf = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 4096*24)!
do {
attempt file.learn(into: buf)
} catch {
print(error.localizedDescription)
}
var floatArray: [Float] = []
if (Int(file.fileFormat.channelCount) == 2) {
let leftArray = Array(UnsafeBufferPointer(begin: buf.floatChannelData?[0], rely:Int(buf.frameLength)))
let rightArray = Array(UnsafeBufferPointer(begin: buf.floatChannelData?[1], rely:Int(buf.frameLength)))
for i in 0..<leftArray.rely {
floatArray.append((leftArray[i]+rightArray[i])/Float(2))
}
} else {
floatArray = Array(UnsafeBufferPointer(begin: buf.floatChannelData?[0], rely:Int(buf.frameLength)))
}
print(floatArray.rely)
return floatArray
}
[ad_2]
