Record and PlayBack audio

Hello,

I want to do recording and playing back audio on my iOS device.

Can someone give me a short hint, which classes are useful for this use case?


I googled around this topic a lot and read a lot of different things.

But I'm not really sure, what classes are the right one for me.


Examples: AVAudioSinkNode, AVAudioSourceNode, AVAudioSession, AVAudioRecorder,

AVAudioPlayer, AVAudioQueue etc. Maybe someone could show me a code snippet or sample project (if possible).


My current state: I would try it with AVAudioRecorder and AVAudioPlayer.


My Recorder class logic:

if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
                let file = dir.appendingPathComponent("audiofile.wav")
                
                // setup audio session with crazy framework
                let format: [String: Any] = [AVSampleRateKey: 44100.0,
                                             AVNumberOfChannelsKey : 2,
                                             AVFormatIDKey: kAudioFormatLinearPCM,
                                             AVEncoderBitRateKey: 320000,
                                             AVEncoderAudioQualityKey : AVAudioQuality.high.rawValue]
                
                recorder = try AVAudioRecorder.init(url: file, settings: format)
                recorder!.prepareToRecord()
            }


My PlayBack class logic:

if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
                let file = dir.appendingPathComponent("audiofile.wav")
                
//                let audioSession = AVAudioSession.sharedInstance()
//                try audioSession.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
                
                player = try AVAudioPlayer(contentsOf: file.standardizedFileURL)
                player!.prepareToPlay()
            }


In the iOS simulator this code works really fine, but not on my iOS (iPhone) device.

The recordPermission is available and valid. But I get no sound on my device.