Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing sound in Apple Watchkit

I'm trying to play a sound through the watchkit extension using WKAudioFilePlayer however no sound gets played. I'm using the haptic feedback code as a sort of debug to be sure it executes it (which it does). As a side note, mute is not on and the filename is correct.

NSURL *falcon = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"falcon"
                                        ofType:@"mp3"]];
WKAudioFileAsset *asset = [WKAudioFileAsset assetWithURL:falcon];
WKAudioFilePlayerItem *sound = [WKAudioFilePlayerItem playerItemWithAsset:asset];
audioPlayer = [WKAudioFilePlayer playerWithPlayerItem:sound];


[audioPlayer play];

WKInterfaceDevice *device = [WKInterfaceDevice currentDevice];
[device playHaptic:WKHapticTypeClick];
like image 201
Rob McMorran Avatar asked Jun 09 '15 22:06

Rob McMorran


People also ask

Can I play a sound on my Apple Watch?

After you connect Apple Watch to Bluetooth headphones or speakers, open the Music app on your Apple Watch, then do any of the following: Play music on your Apple Watch: Turn the Digital Crown to scroll through album artwork, then tap a playlist or album to play it.

How do I listen to audio on Apple Watch?

Open the Apple Watch app on your iPhone. Tap My Watch, then tap Music. Below Playlists & Albums, tap Add Music. Select albums and playlists to sync to your Apple Watch.

Why is my Apple Watch not playing a sound?

Check your Sounds & Haptics settings: On your iPhone, in the Watch app, go to: My Watch > Sounds & Haptics: Check that the Alert Volume is towards the centre or right hand side. Check that Silent Mode is not enabled.

Can you play music directly from Apple Watch speaker?

An Apple Watch provides a convenient way to listen to playlists and albums created on your iPhone. Just a note: You can not play music through your Apple Watch speaker. Only to a device or speaker connected. Your iOS version determines how you listen to your favorite songs on your phone.


1 Answers

I implemented as follows and it worked fine. (Sorry, it's Swift)

1) Define a property for the player object

var player: WKAudioFilePlayer!

2) Setup the asset and player when it's awaking

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)

    let filePath = NSBundle.mainBundle().pathForResource("se_tap", ofType: "m4a")!
    let fileUrl = NSURL.fileURLWithPath(filePath)
    let asset = WKAudioFileAsset(URL: fileUrl)
    let playerItem = WKAudioFilePlayerItem(asset: asset)
    player = WKAudioFilePlayer(playerItem: playerItem)
}

3) Play if the player is ready to play.

@IBAction func playBtnTapped() {
    switch player.status {
    case .ReadyToPlay:
        player.play()
    case .Failed:
        print("failed")
    case .Unknown:
        print("unknown")
    }
}

In addition, it needed to connect a Bluetooth Headset with watch.

like image 57
shu223 Avatar answered Sep 19 '22 12:09

shu223