Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioSession setCategory return alway error

I'm developing music application that simply play song from app. now i'm connecting bluetooth speaker to play. so i'm setting AVAudioSession category, but it will always return error.

Code :

 func setupSessionForPlaying() {
    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(AVAudioSessionCategoryPlayback, with: [.allowBluetooth])
        try audioSession.setActive(true)
    } catch {
        fatalError("Error Setting Up Audio Session")
    }
}

I'm calling this function in didFinishLaunchingWithOptions in AppDelegate.

But if i change setCategory to audioSession.setCategory(AVAudioSessionCategoryPlayback) its working fine.

Does anyone know what's wrong with this code?

Thanks

like image 748
Pratik Prajapati Avatar asked Jan 01 '23 19:01

Pratik Prajapati


1 Answers

According to docs https://developer.apple.com/documentation/avfoundation/avaudiosession/categoryoptions/1616518-allowbluetooth

You can set this option only if the audio session category is playAndRecord or record.

with allowBluetooth you can not use AVAudioSessionCategoryPlayback

Hope it is helpful

like image 130
Prashant Tukadiya Avatar answered Jan 05 '23 17:01

Prashant Tukadiya