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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52626252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:49:58  来源:igfitidea点击:

Unknown selected data source for Port Speaker (type: Speaker)?

swiftxcodeaudio

提问by Khodour.F

i am getting this message in cat log multiple times :

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

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

i am using this code to playback background music :

我正在使用此代码播放背景音乐:

  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)")
        }

i made a research and i found similar issues so i've added the audio category in viewdidload :

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

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

after i've added the above code , the background music is playing even if the phone on silent mode ! and the debugger message for Unknown selected data source for Port Speaker (type: Speaker)is still showing

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

采纳答案by Victor Sanchez

The message Unknown selected data source for Port Speakerseems to be a problem with iOS 12. Apparently it's some warning that appears even if the code is working. Perhaps Apple will fix this soon, so maybe for now you can ignore this warning and once they find a solution you will be able to silence it.

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

Source: AVAudioSession errors in iOS 12

来源:iOS 12 中的 AVAudioSession 错误

As for the background music playing on silent mode, it's because of the AVAudioSessionCategoryyou selected. According to AVAudioSessionCategoryPlaybackdocumentation (source):

至于在静音模式下播放的背景音乐,那是因为AVAudioSessionCategory您选择了。根据AVAudioSessionCategoryPlayback文档(来源):

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

使用此类别时,您的应用音频会在静音开关设置为静音或屏幕锁定时继续播放。

Depending on the style of your app, maybe you could use AVAudioSessionCategorySoloAmbient(source):

根据您的应用程序的风格,也许您可​​以使用AVAudioSessionCategorySoloAmbientsource):

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

您的音频通过屏幕锁定和静音开关(在 iPhone 上称为响铃/静音开关)静音。

Or maybe AVAudioSessionCategoryAmbient(source):

或者也许AVAudioSessionCategoryAmbient来源):

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).

此类别也适用于“一起演奏”风格的应用程序,例如用户在音乐应用程序播放时演奏的虚拟钢琴。当您使用此类别时,来自其他应用程序的音频会与您的音频混合。您的音频通过屏幕锁定和静音开关(在 iPhone 上称为响铃/静音开关)静音。

回答by Adam S.

From Swift 4.2, I originally had it set up like this:

从 Swift 4.2 开始,我最初是这样设置的:

try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [])

I didn't actually need recording capabilities, so I changed it to

我实际上并不需要录音功能,所以我将其更改为

try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])

This removed the error (and was the only thing I could do to get the error gone). However, if you need recording capabilities as well, obviously this won't work.

这消除了错误(这是我唯一能做的事情来消除错误)。但是,如果您还需要录音功能,显然这行不通。