这是我尝试过的
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。
我愿意使用私有 API。不打算将其提交到应用商店;这只是为了我们正在开发的研究生院声学项目。
要将 SwiftUI 应用程序中视图的方向锁定为仅横向,您需要在
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)
}
}
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
在出现之前锁定横向方向,解决您所描述的问题。