Since iOS 6, the recommended approach for this would be using AVAudioSession
(the C-based AudioSession
API is deprecated as of iOS 7).
let currentRoute = AVAudioSession.sharedInstance().currentRoute
currentRoute
returns an AVAudioSessionRouteDescription
, a very simple class with two properties: inputs
and outputs
. Each of these is an optional array of AVAudioSessionPortDescriptions
, which provides the information we need about the current route:
if let outputs = currentRoute?.outputs as? [AVAudioSessionPortDescription] {
// Usually, there will be just one output port (or none), but let's play it safe...
if let airplayOutputs = outputs.filter { $0.portType == AVAudioSessionPortAirPlay } where !airplayOutputs.isEmpty {
// Connected to airplay output...
} else {
// Not connected to airplay output...
}
}
The portType
is the useful info here... see the AVAudioSessionPortDescription
docs for the AVAudioSessionPort...
constants that describe each input/output port type, such as line in/out, built in speakers, Bluetooth LE, headset mic etc.
Also, don't forget to respond appropriate to route changes by subscribing to the AVAudioSessionRouteChangeNotification
.