gpt4 book ai didi

swift - 端口扬声器的未知选定数据源(类型 : Speaker)?

转载 作者:IT王子 更新时间:2023-10-29 05:10:14 33 4
gpt4 key购买 nike

我多次在 cat 日志中收到此消息:

[avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Speaker (type: Speaker)

我正在使用这段代码来播放背景音乐:

  let path = Bundle.main.path(forResource: fileName, ofType:"mp3")!
let url = URL(fileURLWithPath: path)

do {
let sound = try AVAudioPlayer(contentsOf: url)
self.player = sound
sound.prepareToPlay()
sound.volume = 0.05
sound.numberOfLoops = loops
sound.play()
} catch {
print("[PLAY SOUND][DELEGATE] error loading file -> \(fileName)")
}

我进行了研究,发现了类似的问题,所以我在 viewdidload 中添加了音频类别:

  do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}

添加上面的代码后,即使手机处于静音模式,背景音乐也会播放!并且 Unknown selected data source for Port Speaker (type: Speaker) 的调试器消息仍在显示

最佳答案

消息 Unknown selected data source for Port Speaker 似乎是 iOS 12 的问题。显然,即使代码有效,它也会出现一些警告。也许 Apple 会很快解决这个问题,所以也许现在您可以忽略此警告,一旦他们找到解决方案,您就可以将其静音。

来源: AVAudioSession errors in iOS 12

至于静音模式播放的背景音乐,是因为你选择了AVAudioSessionCategory。根据 AVAudioSessionCategoryPlayback 文档(source):

When using this category, your app audio continues with the Silent switch set to silent or when the screen locks.

根据您应用的风格,也许您可​​以使用 AVAudioSessionCategorySoloAmbient ( source ):

Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone).

或者 AVAudioSessionCategoryAmbient ( source ):

This category is also appropriate for “play along” style apps, such as a virtual piano that a user plays while the Music app is playing. When you use this category, audio from other apps mixes with your audio. Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone).

关于swift - 端口扬声器的未知选定数据源(类型 : Speaker)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52626252/

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