问题
As a learning exercise I decided to write a Simon game for Apple Watch (iOS 9.3, WatchKit 2.0).
I created 4 short piano note sounds in GarageBand. I converted them to MP3 files in Audacity, only to find that Apple Watch doesn't support MP3. Ok, great. I need to convert them to WAV or CAF files. Sigh...
Anyway, looking into it, it looks like the only way to play any sounds at all is to display a media controller on the watch. (Using either presentMediaPlayerControllerWithURL:options:completion:
or WKInterfaceMovie
.
I just want to play different 1/4 second piano sounds as the user presses buttons.
Is there any other way with the current WatchKit, that doesn't require displaying a media controller?
回答1:
Using watchOS4 I got it to work using WKInterfaceMovie. It's not that onerous, and much better than presentMediaPlayerControllerWithURL
. Using IB, you need to drag a WKInterfaceInlineMovie object onto the watch face (resize it very small - you won't see it) Connect the object to your WKInterfaceController:
@IBOutlet weak var myMovie: WKInterfaceInlineMovie!
I dragged a couple of buttons onto the interface controller, and call the following with an identifier for the correct sound file, from the IBAction associated with the button:
func playSound(numberOfNote: Int) {
let soundURL = Bundle.main.url(forResource: "note\(numberOfNote)", withExtension: "wav")!
myMovie.setMovieURL(soundURL)
myMovie.playFromBeginning()
}
I click on a button and the sound plays out loud (without earphones) immediately (unlike the other method which has a lengthy pause plus the presented controller needs to be dismissed). This should be perfect for your stated needs. 🍀
来源:https://stackoverflow.com/questions/37463170/is-there-really-no-way-to-play-short-sounds-on-apple-watch-without-a-ui