Play sound on watch

I tried to play sound, putting the files in WatchKit app, then:


let soundURL = NSURL.fileURLWithPath(NSBundle.mainBundle().pathForResource("ShipBullet", ofType:"wav")!)

let asset = WKAudioFileAsset(URL:soundURL)

let sound = WKAudioFilePlayerItem(asset:asset)

let audioPlayer = WKAudioFilePlayer(playerItem:sound)

audioPlayer!.play()

And the app crashed. Am I doing something wrong?

I also tried converting the sound file to AAC, put the ShipBullet.m4a file in both Watch App and Extension, and use this:


if let url = NSBundle.mainBundle().URLForResource("ShipBullet", withExtension:"m4a") {

let asset = WKAudioFileAsset(URL:url)

let sound = WKAudioFilePlayerItem(asset:asset)

audioPlayer = WKAudioFilePlayer(playerItem:sound)

}


But the result is similar, audioPlayer is always nil.

I'm getting one stage further than you, it's trying to play a sound at least (on the simulator I get a 'connect a bluetooth device' Alert) but on the device at the same point the apps interface freezes (but is still running as I get a haptic alert a few seconds later that I've timed) if I don't have a Bluetooth device connected and playing audio if I do.


I'm using caf audio files.


if let audioURL = NSBundle.mainBundle().URLForResource("Audio/Beep5", withExtension: "caf") {

let asset = WKAudioFileAsset(URL: audioURL)

let playerItem = WKAudioFilePlayerItem(asset: asset)

let player = WKAudioFilePlayer(playerItem: playerItem)

switch player.status {

case WKAudioFilePlayerStatus.ReadyToPlay:

player.play()

case WKAudioFilePlayerStatus.Failed:

print(player.error)

case WKAudioFilePlayerStatus.Unknown:

print("unknown")

player.play()

}

}


I can't debug on the watch so I don't know if the state is ReadyToPlay or Unknown, hence the .play() there too

Still waiting for my phone to update to IOS 9 so I can test it...but here is the code apple gave on thier transition page:



  1. NSBundle* myBundle = [NSBundle mainBundle];
  2. NSURL* movieURL = [myBundle URLForResource:@"myMovie" withExtension:@"mp4"];
  3. NSDictionary* options = @{WKMediaPlayerControllerOptionsAutoplayKey : @YES};
  4. [self presentMediaPlayerControllerWithURL:movieURL
  5.   options:options
  6.   completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * __nullable error) {
  7.   if (error)
  8.   NSLog(@"Error: %@", [error description]);
  9.   /
  10.   }];

I can also confirm all of the above problems. I have tried caf, wav, and m4a.

Also getting crashes on device, the simulator asks for a bluetooth audio device however.

So was able to get some results. Using the following code from above:

if let audioURL = NSBundle.mainBundle().URLForResource("Audio/Beep5", withExtension: "caf") {

let asset = WKAudioFileAsset(URL: audioURL)

let playerItem = WKAudioFilePlayerItem(asset: asset)

let player = WKAudioFilePlayer(playerItem: playerItem)

switch player.status {

case WKAudioFilePlayerStatus.ReadyToPlay:

player.play()

case WKAudioFilePlayerStatus.Failed:

print(player.error)

case WKAudioFilePlayerStatus.Unknown:

print("unknown")

player.play()

}

}

BUT, it doesn't work on the simulator and on the device itself it does require the watch be paired with a bluetooth speaker (which I think defeats the purpose). Not only that, it doesn't work 100% either. I have an app with about 20 different sounds and maybe one or two play correctly about 20% of the time. The reason I can hear hissing on the speaker but no sounds. All my files are mp3, and I haven't tried any other format. But might write another test app to try it out.

I think we need to look for the method to tell it to use the watch speaker instead of the bluetooth. Hopefully Apple will fix that soon.

Exactly, I did find some sample code which plays an audio file, however this is by no means a solution as it has to open the media player in order to do so. I need a background audio player that doesn't interfere with the interface.


    NSDictionary *options = @{
                              WKMediaPlayerControllerOptionsAutoplayKey : @YES
                              };
    NSURL* falcon = [[NSBundle mainBundle] URLForResource:@"falcon" withExtension:@"mp3"];
   
    [self presentMediaPlayerControllerWithURL:falcon 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);
        }
       
        if (error) {
            NSLog(@"There was an error with playback: %@.", error);
        }
    }];

Yeah on their transistion page I found it odd how they are giving example in Cocco instead of Swift which they are heavily promoting. Hopefully we'll figure out how to play sounds on the speaker soon.

I tried and here's what I found:


- the sound will only works with Bluetooth speaker attached, if not, the UI will freeze.

- the sound can only be played once (ReadyToPlay).

Do you have better luck in beta 3?

Does anybody got any luck in playing sound in beta 5?


My app will hung during launched when it encountered these statements (this works in beta 2):


for step in steps {

let url = NSURL.fileURLWithPath(NSBundle.mainBundle().pathForResource(step.id, ofType: "m4a")!)

let asset = WKAudioFileAsset(URL: url)

let playerItem = WKAudioFilePlayerItem(asset: asset)

players.append(WKAudioFilePlayer(playerItem: playerItem))

}

Similar code freezes the UI after playing in my case, both in simulator & real device. The code was running without problems in previous betas. :/

And yes, it would be nice to have a 'background' player routing sound to the speaker.

I got it to play, but it doesn't work like I wanted it to. Basically in using Apple's example you have to drop I think it's a movie button (not at my machine so can't look it up) in the program and use that to play the sounds. But instead of actually just playing the sound, it opens up another player window whichi will then play the sound. So not exactly what I'm trying to do in my app. So hopefully in the future we'll get more direct access to play sounds.


UPDATE: You add a WKInterfaceMovie to the app to play the sounds

Play sound on watch
 
 
Q