'Detect the AirPlay Device Type iPhone is Connected To

Is there a way to get the AirPlay device type the iPhone is connected to? For instance, is there a way to know that the iPhone is connected to a HomePod, an Apple TV, or AirPods via AirPlay? I would like to show an icon in my app for which device the iPhone is connected to (I provide the icons). I have somewhat achieved this using

let currentRoute = AVAudioSession.sharedInstance().currentRoute
let output = currentRoute.outputs.first!
let portType = output.portType

but AVAudioSession.Port only contains the type airPlay (see link for all property types). It does not contain something like homepod, appleTV, .airpods, etc. Does anyone have a work around for this or any ideas?



Solution 1:[1]

Just ran into this myself, In addition to checking for AVAudioSession.Port.airPlay of the current AVAudioSession, you also need to check the value of the AVPlayer's isExternalPlaybackActive property. If it's true, it's AirPlaying video, if not it's just sending the audio to the HomePod or headphones.

Solution 2:[2]

To do this, I would make an array of all of the values

values = [[text1, val1], [text2, val2], [text3, val3], [name, name_interface]]
# name and name_interface are the last two values

Then, I would make the function take a list of values and create a base dictionary for adding values into.

def merge_variables_to_unique_object(values: list):
    base_json = {
        "network": {
            "routes": {
                "vlan": [
                    
                ]
            },
            "interface": {
                
            }
        }
    }

After that, it's just a simple for loop that goes until the length of the values array. Inside of the array, you check if you are at the last element of the array. If you are not at the end, you add the values to the array (remember that vlan is a dictionary)

base_json["network"]["routes"]["vlan"].append({values[i][0]: values[i][1]})

If you are at the last element of the array, you add the values to interface:

base_json["network"]["interface"][values[i][0]] = values[i][1]

The for loop would look like this:

for i in range(len(values)):
    if i < len(values) - 1:
        base_json["network"]["routes"]["vlan"].append({values[i][0]: values[i][1]})
    else:
        base_json["network"]["interface"][values[i][0]] = values[i][1]

Afterwards, you would just return base_json because it was changed inside of the for loop.

Solution 3:[3]

If you want all the content in this syntactical format you can simply write it to an file or an string. Either way it will be something like this. This is manually creating the syntax you want that you can write to an file to force through .json.

input1 = text1.split(".") input2 = text2.split(".") and so on

str1="[{\n\tinput1[1]: {\n\t\tinput1[2] : {\n\t\t\tinput1[3]" in this fashion

\t are tabs like indenting in code \n is interpreted in strings as next line.

Is this helpful?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CA Bearsfan
Solution 2 Connor Morrison
Solution 3 Christopher Hoffman