'Airplay not working with AVRoutePicker with Swift but does with Objective C

I've been creating an app in objective C that can stream to an AirPlay device and this works. I've then tried to convert this over to swift as I have some other functionality that I've already made, but for some reason playback is only through the device and not the AirPlay device and I have no idea why.

Can anyone see the difference between my Objective C and Swift below? and what I might be doing wrong:

#import "ViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()  <AVRoutePickerViewDelegate>

@end

@implementation ViewController

AVAudioPlayer *player;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (@available(iOS 11.0, *)) {
        AVRoutePickerView *routerPickerView = [[AVRoutePickerView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
        routerPickerView.activeTintColor = [UIColor clearColor];
        routerPickerView.delegate = self;
        [self.view addSubview:routerPickerView];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:routerPickerView.bounds];
        imageView.image = [UIImage imageNamed:@"logo2"];
        [routerPickerView addSubview:imageView];
    } else {
        // Fallback on earlier versions
    }

    //code to play audio
    NSString *soundFilePath = [NSString stringWithFormat:@"%@/song.mp3",[[NSBundle mainBundle] resourcePath]];
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
    player.numberOfLoops = -1;
    [player play];
}
@end

Swift:

import UIKit
import AVFoundation
import AVKit

class ViewController: UIViewController, AVAudioRecorderDelegate, UITableViewDelegate, UITableViewDataSource, AVRoutePickerViewDelegate {
    
    var player: AVAudioPlayer!

 override func viewDidLoad() {
        super.viewDidLoad()
        
        if #available(iOS 11.0, *) {
            let routerPickerView = AVRoutePickerView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
            routerPickerView.activeTintColor = UIColor.clear
            routerPickerView.delegate = self
            view.addSubview(routerPickerView)

            let imageView = UIImageView(frame: routerPickerView.bounds)
            imageView.image = UIImage(named: "logo2")
        
         
            //code to play audio
            let soundFilePath = "\(Bundle.main.resourcePath ?? "")/song.mp3"
            let soundFileURL = URL(fileURLWithPath: soundFilePath)
            do {
                player = try AVAudioPlayer(contentsOf: soundFileURL)
            } catch {
            }
            player.numberOfLoops = -1
            player.play()
}

Not sure if there is a setting i may have missed or something as simple as that, so any recommendations are welcome.



Solution 1:[1]

Maybe because you didn't translate the line [routerPickerView addSubview:imageView];

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 Greg Anderson