gpt4 book ai didi

ios - 静音/静音模式打开时声音仍在播放

转载 作者:行者123 更新时间:2023-11-28 06:20:38 31 4
gpt4 key购买 nike

我正在使用 AudioServicesPlaySystemSoundWithCompletion 播放系统声音,并使用 AVAudioPlayer 播放自定义声音。

为什么打开静音开关会播放声音?我已经尝试了“AVAudioSession”的所有类别。

我尝试了以下代码:

 try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategorySoloAmbient)
try AVAudioSession.sharedInstance().setActive(true)

也试过这个:

try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.mixWithOthers)
try AVAudioSession.sharedInstance().setActive(true)

我尝试了此链接中定义的所有类别,但是当手机静音(静音开关打开)时,声音总是在播放。 https://developer.apple.com/reference/avfoundation/avaudiosession/audio_session_categories

我正在播放这样的声音:

系统声音:

AudioServicesPlaySystemSoundWithCompletion(1304, nil)

自定义声音:

        let url = Bundle.main.url(forResource: "sound01", withExtension: ".wav")
do {
if audioPlayer != nil {
audioPlayer!.stop()
audioPlayer = nil
}
if let soundURL = soundURL {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.mixWithOthers)
try AVAudioSession.sharedInstance().setActive(true)
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
}
guard let audioPlayer = audioPlayer else {
return
}
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch let error {
print(error.localizedDescription)
}

我做错了什么吗?

最佳答案

修复是使用 AudioServicesPlayAlertSound 函数而不是 AudioServicesPlaySystemSoundWithCompletion 函数。

我们也不能使用 AVAudioPlayer。我们需要通过为每个自定义声音创建声音 ID 来使用 AudioServicesPlayAlertSound 函数。

let soundURL = Bundle.main.url(forResource: "test01", withExtension: ".wav")

if let soundURL = soundURL {
var soundID : SystemSoundID = 0
AudioServicesCreateSystemSoundID(soundURL as CFURL, &soundID)
AudioServicesPlayAlertSound(soundID)
}

关于ios - 静音/静音模式打开时声音仍在播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43638790/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com