Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xcode

swift

audio

i am getting this message in cat log multiple times :

[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 :

  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

like image 555
Khodour.F Avatar asked Oct 03 '18 11:10

Khodour.F


2 Answers

The message Unknown selected data source for Port Speaker seems 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.

Source: AVAudioSession errors in iOS 12

As for the background music playing on silent mode, it's because of the AVAudioSessionCategory you selected. According to AVAudioSessionCategoryPlayback documentation (source):

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

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

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

like image 60
Victor Sanchez Avatar answered Nov 03 '22 02:11

Victor Sanchez


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

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.

like image 32
Adam S. Avatar answered Nov 03 '22 00:11

Adam S.