Speaker / Bluetooth playback output processing

I am trying to play an audio file and handle any changes that might occur. If I start playing an audio file, it plays on the bluetooth speakers. If I turn off the speakers, it plays on the speaker of the phone. However , if I turn the bluetooth speakers back on, the notification method is started, but it doesn't see the bluetooth speakers.

Every time I turn on the bluetooth speakers, the notification is notified, the method works, but the bluetooth speaker is not listed in the outputs.

Notice:

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("myRouteChangeSelector"), name: AVAudioSessionRouteChangeNotification, object: nil)

      

with method code:

func myRouteChangeSelector(){

    let currentRoute = AVAudioSession.sharedInstance().currentRoute
    for route in currentRoute.outputs {
        println("PortType \(route.portType), Description \(route.portName)")
        if route.portType == "BluetoothA2DPOutput"{
            println("Bluetooth detected")
            let portDescription = route as! AVAudioSessionPortDescription
            AVAudioSession.sharedInstance().setPreferredInput(portDescription, error: nil)
        } else {
            println("No Bluetooth, speaker only")
            AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker, error: nil)
        }

    }
}

      

+3


source to share





All Articles