UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How do I ensure AVAudioPlayer plays through physical speaker on Apple Watch?

Forums > watchOS

I recently followed this tutorial (https://www.youtube.com/watch?v=Y8A_Wkcx83Q) from Paul about playing sounds on watchkit using AVFoundation and AVAudioPlayer. It was very helpful, and I now have custom sounds playing on my own unique watchkit app!

Unfortunately, however, I am only able to hear these sounds play via the simulator on my computer. On my physical watch, no sound comes through.

To make matters even more confusing, different users who are testing my app via TestFlight seem to have different experiences with my custom sounds. Some (like me) can't hear them at all, some CAN hear them but only via bluetooth speakers, some can only hear them if their watch is not on silent mode, and others can only hear the sounds if their watch screen stays active.

In my app, I would like to be able to play a sound with a slight delayed start and have that sound play on the watch's external speaker regardless of whether the screen is asleep or not.

To accomplish this, I've tried adding background audio capabilities to both the iOS version and watchOS version of my app. I've also tried creating an AVAudioSession variable and tried adding the following lines of code before my AVAudioPlayer plays my actual sound:

   do {

       try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionPortBuiltInSpeaker,
       routeSharingPolicy: AVAudioSessionRouteSharingPolicy.default, options: [])

    } catch {
        print("audiosession cannot be set")
    }

   do {
        try audioSession.setActive(true) }
    catch {
        print ("audiosession cannot be activated")
    }

However, I have still not accomplished any consistent playing of sound via the Apple Watch's speaker on a physical device. Thank you for your help!

6      

Hi @dvoland, I'm not sure what your instance of audioSession is, but be sure you are accessing the shared instance. I experienced a similar problem, but this fixed it for me.

try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try? AVAudioSession.sharedInstance().setActive(true)

Hope that helps!

7      

I've hit a similar issue. Any insights or suggestions would be greatly appreciated.

  1. Works as expected in the simulator.
  2. Plays through the speaker on watchOS 7, but not watchOS 8.
  3. Bluetooth audio works as expected.
  4. If app is playing audio (still no sound), "Apple Watch" dissapears from the AirPlay audio list.
  5. Haptics and associated sounds work as expected.
  6. Edited session volume is stuck at 0

I was able to get some debugger output with some category changes:

try session.setCategory(.playback, mode: .default, policy: .longFormAudio, options: [])

Buffer: <AVAudioFormat 0x156778f0:  1 ch,  48000 Hz, Float32> <AVAudioPCMBuffer@0x1556b330: 3840/3840 bytes>
Buffer: <AVAudioFormat 0x156778f0:  1 ch,  48000 Hz, Float32> <AVAudioPCMBuffer@0x1556aec0: 3840/3840 bytes>

[BTAudioSession] ### Activate failed: kUnsupportedErr (SmartRouting is disabled)
[BTAudioSession] ### Error: kUnsupportedErr (SmartRouting is disabled)

Buffer: <AVAudioFormat 0x1551b440:  2 ch,  44100 Hz, Float32, non-inter> <AVAudioPCMBuffer@0x1551c6b0: 3528/3528 bytes>
Buffer: <AVAudioFormat 0x1551b440:  2 ch,  44100 Hz, Float32, non-inter> <AVAudioPCMBuffer@0x1551b5f0: 3528/3528 bytes>

[BTAudioSession] ### Activate failed: kUnsupportedErr (SmartRouting is disabled)
[BTAudioSession] ### Error: kUnsupportedErr (SmartRouting is disabled)
[BTAudioSession] ### Activate failed: kUnsupportedErr (SmartRouting is disabled)
[BTAudioSession] ### Error: kUnsupportedErr (SmartRouting is disabled)

Update

I added a list of AVAudioSession.sharedInstance() properties, and realized the system volume was permanently set to 0 for the speaker. I added a WKInterfaceVolumeControl as suggested on StackOverflow, and was able to change the volume to 1.0. Now my app and others can play through the speaker again. This seems like a watchOS bug to me.

5      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.