【问题标题】:Airplay not working with AVRoutePicker with Swift but does with Objective CAirplay 不能与 Swift 的 AVRoutePicker 一起使用,但可以与 Objective C 一起使用
【发布时间】:2021-10-19 19:38:34
【问题描述】:

我一直在目标 C 中创建一个可以流式传输到 AirPlay 设备的应用程序,这很有效。然后我尝试将其转换为 swift,因为我已经制作了一些其他功能,但由于某种原因,播放只能通过设备而不是 AirPlay 设备,我不知道为什么。

谁能看到下面我的 Objective C 和 Swift 之间的区别?以及我可能做错了什么:

#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

斯威夫特:

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

不确定是否有我可能错过的设置或这么简单的设置,因此欢迎任何建议。

【问题讨论】:

    标签: ios swift objective-c xcode


    【解决方案1】:

    也许是因为你没有翻译这行 [routerPickerView addSubview:imageView];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 2012-07-14
      • 1970-01-01
      相关资源
      最近更新 更多