处理播放输出到扬声器/蓝牙

时间:2015-05-20 22:10:29

标签: ios swift bluetooth avfoundation

我尝试播放音频文件并处理可能发生的任何更改。如果我开始播放音频文件,它将在蓝牙音箱上播放。如果我关闭扬声器,它将在手机扬声器上播放。 然而,如果我重新打开蓝牙音箱,会运行通知方法,但它没有看到蓝牙音箱。

每当我打开蓝牙音箱时,通知都会收到提醒,方法会运行,但蓝牙音箱并未列在输出中。

通知:

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

方法代码为:

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)
        }

    }
}

0 个答案:

没有答案
相关问题