IOS: AVAudioSession not working IOS: AVAudioSession not working ios ios

IOS: AVAudioSession not working


AVAudioSession has some changes in Swift 4.2

under viewDidAppear(), try this:

    // Prepare Audio Session    self.audioSession = AVAudioSession.sharedInstance()    try audioSession.setCategory(AVAudioSession.Category.playAndRecord, mode: .measurement, options: .defaultToSpeaker)    try audioSession.setActive(true, options: .notifyOthersOnDeactivation)

It worked for me when I converted the language to Swift 4.2 in Xcode 10You can do that by going to...

Edit, Convert, To Current Swift Syntax...


Paste in viewWillAppear()AVAudioSession has some changes, the corrected Syntax for Swift 5.0Declare var recordingSession: AVAudioSession!

recordingSession = AVAudioSession.sharedInstance()        do {            try recordingSession.setCategory(.playAndRecord, mode: .spokenAudio, options: .defaultToSpeaker)            try recordingSession.setActive(true, options: .notifyOthersOnDeactivation)            recordingSession.requestRecordPermission() { [unowned self] allowed in                DispatchQueue.main.async {                    if allowed {                        self.loadRecorder()                    } else {                        // failed to record!                    }                }            }        } catch {            // failed to record!        }`