Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can't access .playAndRecord

I'm trying to play a sound in Swift 4 iOS 12 with Xcode 10. I have a project where I already did that and I'm copying the code from that project.

But now when executing the following code:

try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playAndRecord, mode: .default)

I get the following error in this exact line:

Type 'AVAudioSession.Category' (aka 'NSString') has no member 'playAndRecord'

I already imported AVFoundation so I have no idea is not working.

like image 248
JSChrono Avatar asked Jan 06 '19 01:01

JSChrono


2 Answers

I face the same issue, I was working in swift v4 and the code you are using is for Swift 4.2 You have to use the following string AVAudioSessionCategoryPlayAndRecord in place of playAndRecord

let audioSession = AVAudioSession.sharedInstance()

    do {
        try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeVoiceChat, options: AVAudioSessionCategoryOptions.mixWithOthers)
        try audioSession.overrideOutputAudioPort(.none)
        try audioSession.setActive(true)
    } catch {
        debugPrint(error.localizedDescription)
    }

}
like image 150
Kamal Bhardwaj Avatar answered Nov 05 '22 15:11

Kamal Bhardwaj


you can use 'AVAudioSessionCategoryPlayAndRecord'

like image 28
cqswzqcj Avatar answered Nov 05 '22 16:11

cqswzqcj