Play sound on watchOS 3

Does anybody got any luck playing sound on Apple Watch? I have tried since watchOS 2 and never get to work. In watchOS 3 documentation (https://developer.apple.com/library/prerelease/content/documentation/General/Conceptual/WatchKitProgrammingGuide/AudioandVideo.html#//apple_ref/doc/uid/TP40014969-CH24-SW19), it says:


Any audio you play is routed to a paired Bluetooth headset if one is available. If no Bluetooth headset is available, audio is routed to the Apple Watch speaker.


Here's my code - instead of hearing the sound, the UI hung. I have placed the sound asset ShipHit.m4a in both Watch App and Watch Extension for testing purpose.


        let url = NSURL.fileURL(withPath: Bundle.main().pathForResource("ShipHit", ofType: "m4a")!)
        let asset = WKAudioFileAsset(url: url)
        let playerItem = WKAudioFilePlayerItem(asset: asset)
        soundPlayer = WKAudioFilePlayer(playerItem: playerItem)
...
        if soundPlayer.status == .readyToPlay {
            soundPlayer.play()
        }

Same for me, impossible to play sound on Apple Watch speaker 😕

It works for me on Apple Watch OS 3 Beta 2.


I modified the WatchPuzzle sample code which Apple provided for testing its speaker.


Here is code snippet

In InterfaceController.swift I added some code below.

    func startGame() {
        guard let gameNodes = gameNodes else { fatalError("Nodes not set") }
  
        guard let soundSource = SCNAudioSource(named:"art.scnassets/computer.mp3") else { fatalError("Audio File NOT found") }
  
        let startSequence = SCNAction.sequence([
          
            SCNAction.wait(forDuration: 1.0),
      
            SCNAction.play(soundSource, waitForCompletion: false),
      
            SCNAction.group([
              
                SCNAction.fadeIn(withDuration: 0.3),


and added the MP3 file to play in background to art.scnassets folder. If you add a big-sized mp3 file, installation time of the watch extension would be long.


I hope it will be helpful to you.

Thanks for response.


Is using SceneKit or SpriteKit to play sound only the only way (I have tried SprikeKit and it works)? Can we use WKAudioFilePlayer?

Would you be able to post more of your code above. I'm trying to find the simplest way to play a custom sound on the watch speakers and currently your claim that you have it working is the only one I have found. But can't repro as i don't have more of your code. So far I have this and it doesn't work for me:



SCNNode * audioNode = [[SCNNode alloc] init];

SCNAudioSource * audioSource = [SCNAudioSource audioSourceNamed:@"mysound.mp3"];

SCNAudioPlayer * audioPlayer = [SCNAudioPlayer audioPlayerWithSource:audioSource];

[audioNode addAudioPlayer:audioPlayer];


SCNAction * play = [SCNAction playAudioSource:audioSource waitForCompletion:YES];

[audioNode runAction:play];

Using WKAudioFilePlayer works well for me on the simulator.

The file 'test.mp3' is in the WatchKit Extension Bundle.


Here is what I did:


let mainBundle = Bundle.main
if let url = mainBundle.url(forResource: "test", withExtension: "mp3"){
    let asset = WKAudioFileAsset(url: url)
    let playerItem = WKAudioFilePlayerItem(asset: asset)
    soundPlayer = WKAudioFileQueuePlayer(playerItem: playerItem)
    if soundPlayer.status == .readyToPlay {
        soundPlayer.play()  
}

I also can't get any sound to play via the iPhone or the Watch's speakers. My watchos 1 app could play sound on the iPhone triggered from the watch using avaudioplayer but in watchos 3, avaudioplayer isn't allowed so now my app which is based around sound, can't play any sounds.


Any help to fix this would be appreciated!

I have managed to get it to work through the Watch Speakers by triggering the WKMediaPlayer using a button.


As per Apple's guidelines, sounds must be 32bit stereo.


Code used in InterfaceController.m:


- (IBAction)playSound1 {
  
  
    NSDictionary *options = @{
                              WKMediaPlayerControllerOptionsAutoplayKey : @YES
                              };
    NSURL *assetURL = [[NSBundle mainBundle] URLForResource:@"1" withExtension:@"wav"];
    [self presentMediaPlayerControllerWithURL:assetURL options:options completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * __nullable error) {
        if !(didPlayToEnd) {
          
          
          
            NSLog(@"The player did not play all the way to the end. The player only played until time - %.2f.", endTime);
          
        }
      
    }];
}
Play sound on watchOS 3
 
 
Q