Play sound from Apple Watch speaker
This is now possible as of watchOS 2 using WKAudioFilePlayer
or WKInterfaceMovie
.
NSURL *assetURL = [[NSBundle mainBundle] URLForResource:@"file" withExtension:@"wav"];
WKAudioFilePlayer
example:
WKAudioFileAsset *asset = [WKAudioFileAsset assetWithURL:assetURL];
WKAudioFilePlayerItem *playerItem = [WKAudioFilePlayerItem playerItemWithAsset:asset];
WKAudioFilePlayer *audioFilePlayer = [WKAudioFilePlayer playerWithPlayerItem:playerItem];
[audioFilePlayer play];
WKInterfaceMovie
example:
[self presentMediaPlayerControllerWithURL:assetURL options:nil completion:nil];
It is not possible to play sound out of the Apple Watch's speaker, but you can trigger playing a sound file on the iPhone, here is thread about it
import AVFoundation
var player: AVAudioPlayer?
if let path = Bundle.main.path(forResource: "siren", ofType: "wav") {
let fileUrl = URL(fileURLWithPath: path)
do{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(contentsOf: fileUrl)
guard let player = player else { return }
player.play()
}
catch
{
}
}
I've used this to play a custom sound from the apple watch(4.3) speaker and worked just fine. Don't forget to set the audio file target membership to the watch kit.