简体   繁体   English

Swift File.read(into: ) 抛出“Foundation._GenericObjCError 错误 0”错误

[英]Swift File.read(into: ) throws "Foundation._GenericObjCError error 0" error

Im working on a project that consists of recording a watermelon thump and predict its sweetness by the sound it produces.我正在做一个项目,该项目包括录制西瓜的砰砰声,并通过它产生的声音预测它的甜度。 Im using AVAudioRecorder to record and save the audio.我使用 AVAudioRecorder 录制和保存音频。 but when I try to open and read the file it gives me an error (This error only occurs when I test the app on my actual iPhone Xr but not when I use the xcode simulator) the error code is:但是当我尝试打开并读取文件时,它给了我一个错误(这个错误只发生在我在我的实际 iPhone Xr 上测试应用程序时,而不是当我使用 xcode 模拟器时)错误代码是:

"Foundation._GenericObjCError error 0" . “Foundation._GenericObjCError 错误 0”。

Here is the recording function and fetching audios function:这是录音功能和获取音频功能:

private func startRecording() {
        do {
            
            if self.record{
                // recording already started
                
                recorder.stop()
                self.record.toggle()
                // updating data for every recording
                self.getAudios()
                return
            }
            // record audio
            
            // store audio in document directory
//            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.main.url(forResource: "thump", withExtension: "wav")
//            }()!
            
            let recordSettings: [String : Any] = [AVFormatIDKey: Int(kAudioFormatLinearPCM),
                                                  AVSampleRateKey: 44100.0,
                                                  AVNumberOfChannelsKey: 1,
                                                  AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue ]
            
            self.recorder = try AVAudioRecorder(url: fileURL, settings: recordSettings)
            print(fileURL)
            self.recorder.record()
            print("Recording started")
            self.record.toggle()
        } catch {
            print(error.localizedDescription)
        }
    } 

func getAudios() {
        
        do {
            
            let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
            
            // Fetch all data from document directory
            
            let result = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: .producesRelativePathURLs)
            
            self.audios.removeAll()
            
            for i in result {
                
                self.audios.append(i)
                
            }
            
        }
        catch {
            print(error.localizedDescription)
        }
        
    } 

And this is the function where I get the error in the file.read(into:buf):这是我在 file.read(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.main.url(forResource: "thump", withExtension: "wav")
            print(fileName)
            let file = try! 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 {
                try file.read(into: buf)
            } catch {
                print(error.localizedDescription)
            }
            
            var floatArray: [Float] = []

            if (Int(file.fileFormat.channelCount) == 2) {
                let leftArray = Array(UnsafeBufferPointer(start: buf.floatChannelData?[0], count:Int(buf.frameLength)))
                let rightArray = Array(UnsafeBufferPointer(start: buf.floatChannelData?[1], count:Int(buf.frameLength)))
                
                for i in 0..<leftArray.count {
                    floatArray.append((leftArray[i]+rightArray[i])/Float(2))
                }
            } else {
                
                floatArray = Array(UnsafeBufferPointer(start: buf.floatChannelData?[0], count:Int(buf.frameLength)))
                
            }
            
            print(floatArray.count)
            return floatArray

        }

更新:我通过将会话的类别设置为 playandRecord 来解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM