简体   繁体   中英

Playing sound from Apple Watch through iPhone

I'm trying to incorporate sound effects into an Apple Watch application I've been working on (written in Objective-C), but I was disappointed by the fact that third party applications are not permitted to play sounds directly from the watch. As a workaround, I was looking to see if there was a way to play a specified sound through the paired device's speakers. I found one such thread here:

Play sound from Apple Watch speaker

however the link in the verified answer seems to be down at this time. In short, how can I play a sound effect triggered by the Apple Watch through its paired iPhone's speakers? Also, is it possible to make the device vibrate from the watch? Any help is greatly appreciated.

Unfortunately, AppleWatch doesn't allow developers to play or to record audio. You can play sound from iPhone speaker, as the simulator does. If you implement AVAudioPlayer on you WatchKit extension, you are going to listen in your Mac because the iPhone simulator is playing for you.

Taken from Apple Developer Forum:

Since the code simplegames showed is running on the iPhone simulator, not the watch simulator, and the button press on the watch simulator is causing the iPhone simulator code to activate the audio player, your mac is simulating sound from the iPhone.

No. It is not possible to play sounds with WatchKit on the Apple Watch.

I got the watch to play audio like this. It does however present the media player. Its not ideal but it works.

#import <Foundation/Foundation.h>

NSURL *yourURL = [myBundle URLForResource:@"yourAudioFile" withExtension:@"wav"];

NSDictionary *options = @{WKMediaPlayerControllerOptionsAutoplayKey : @YES};

[self presentMediaPlayerControllerWithURL:yourURL options:options completion:^(BOOL didPlayToEnd, NSTimeInterval endTime, NSError * __nullable error) {if (error)NSLog(@"Error: %@", [error description]);}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM