Is there REALLY no way to play short sounds on Apple watch without a UI?

时光怂恿深爱的人放手 提交于 2019-12-11 13:56:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!