From 21c6d61c89907e17c9da63b82dbafeafe8e1ce2d Mon Sep 17 00:00:00 2001 From: kintan Date: Mon, 6 Nov 2023 10:56:06 +0800 Subject: [PATCH] fix #609 --- Sources/KSPlayer/AVPlayer/KSOptions.swift | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Sources/KSPlayer/AVPlayer/KSOptions.swift b/Sources/KSPlayer/AVPlayer/KSOptions.swift index eb15e6984..41334d89a 100644 --- a/Sources/KSPlayer/AVPlayer/KSOptions.swift +++ b/Sources/KSPlayer/AVPlayer/KSOptions.swift @@ -507,6 +507,10 @@ public extension KSOptions { KSLog("[audio] isSpatialAudioEnabled: \(isSpatialAudioEnabled)") let isUseAudioRenderer = KSOptions.audioPlayerType == AudioRendererPlayer.self KSLog("[audio] isUseAudioRenderer: \(isUseAudioRenderer)") + let maxRouteChannelsCount = AVAudioSession.sharedInstance().currentRoute.outputs.compactMap { + $0.channels?.count + }.max() ?? 2 + KSLog("[audio] currentRoute max channels: \(maxRouteChannelsCount)") var channelCount = channelCount if channelCount > 2 { let minChannels = min(maximumOutputNumberOfChannels, channelCount) @@ -514,15 +518,14 @@ public extension KSOptions { try? AVAudioSession.sharedInstance().setPreferredOutputNumberOfChannels(Int(minChannels)) KSLog("[audio] set preferredOutputNumberOfChannels: \(minChannels)") } + // iOS 有空间音频功能,所以不用处理 + #if os(tvOS) || targetEnvironment(simulator) if !(isUseAudioRenderer && isSpatialAudioEnabled) { - let maxRouteChannelsCount = AVAudioSession.sharedInstance().currentRoute.outputs.compactMap { - $0.channels?.count - }.max() ?? 2 - KSLog("[audio] currentRoute max channels: \(maxRouteChannelsCount)") // 不要用maxRouteChannelsCount来panduan,有可能会不准。导致多音道设备也返回2(一开始播放一个2声道,就容易出现) // channelCount = AVAudioChannelCount(min(AVAudioSession.sharedInstance().outputNumberOfChannels, maxRouteChannelsCount)) channelCount = AVAudioChannelCount(AVAudioSession.sharedInstance().outputNumberOfChannels) } + #endif } else { channelCount = 2 }