流式传输到 Apple TV 时如何将音频路由到 AirPods?

问题描述 投票:0回答:1

这是我尝试过的

func configureAudioSession() {
    let session = AVAudioSession.sharedInstance()
    do {
        try session.setCategory(.playAndRecord, options: [.mixWithOthers, .allowBluetoothA2DP, .defaultToSpeaker])
        try session.setActive(true)
        print("Session is active. Current route: \(session.currentRoute)")

        if let bluetoothOutput = session.currentRoute.outputs.first(where: { $0.portType == .bluetoothA2DP }) {
            print("Bluetooth A2DP device available: \(bluetoothOutput.portName)")
            // Optionally prompt user or handle routing preferences
        } else {
            print("No Bluetooth A2DP route available; consider manual routing via Control Center.")
        }
        
    } catch {
        print("Failed to configure audio session: \(error)")
    }
}

本质上,如果我的 iPhone 将其音频流式传输到附近的 Apple TV,我希望应用程序的音频通过一对 AirPods 而不是 Apple TV。

  • 我不想将 AirPods 连接到 Apple TV
  • 我尝试了上面的代码,但最终将整个系统音频重定向到 AirPods/设备内置扬声器。我不希望这样——相反,我希望系统音频继续传输到 Apple TV,而我的应用程序可以以某种方式将其音频发送到 AirPods。

我愿意使用私有 API。不打算将其提交到应用商店;这只是为了我们正在开发的研究生院声学项目。

ios swift objective-c avaudiosession avaudioengine
1个回答
0
投票

要将 SwiftUI 应用程序中视图的方向锁定为仅横向,您需要在

UIViewController
中或通过基于场景的更新来管理方向。下面是一种简化的方法,可确保视图在出现之前锁定在横向中。

代码示例:

  1. 创建自定义 UIViewController 来管理横向方向:
import SwiftUI

struct LandscapeOnlyView<Content: View>: UIViewControllerRepresentable {
    let content: Content

    init(@ViewBuilder content: () -> Content) {
        self.content = content()
    }

    func makeUIViewController(context: Context) -> UIViewController {
        let controller = UIHostingController(rootView: content)
        controller.modalPresentationStyle = .fullScreen
        return LandscapeViewController(rootViewController: controller)
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}

class LandscapeViewController: UIViewController {
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscape
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene
        scene?.requestGeometryUpdate(.iOS(interfaceOrientations: .landscapeRight), errorHandler: nil)
    }
}
  1. 使用此视图作为特定于景观的内容的包装:
struct ContentView: View {
    @State private var isLandscapeViewPresented = false

    var body: some View {
        Button("Open Landscape View") {
            isLandscapeViewPresented.toggle()
        }
        .fullScreenCover(isPresented: $isLandscapeViewPresented) {
            LandscapeOnlyView {
                Text("This view is locked in landscape orientation")
                    .padding()
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    .background(Color.blue)
                    .foregroundColor(.white)
            }
        }
    }
}

这可确保

LandscapeView
在出现之前锁定横向方向,解决您所描述的问题。

© www.soinside.com 2019 - 2024. All rights reserved.