且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为手表套件应用播放声音

更新时间:2023-02-26 21:47:59

目前没有任何方法可以在 Watch 上播放音频.你的假设是 100% 正确的.Apple 不想在运行时将音频剪辑从 Watch Extension 传输到 Watch.目前,唯一支持的传输缓存系统是 图像.现在,您必须在 iOS 设备上播放音频.您可以使用以下方法之一触发播放声音:

There currently isn't any way to play audio on the Watch. Your assumptions are 100% correct. Apple doesn't want to have to transfer the audio clips from the Watch Extension to the Watch at runtime. Currently, the only supported transfer caching system is for images. For now, you will have to play the audio on the iOS device. You can use one of the following approaches to trigger playing the sound:

  • openParentApplication:reply: - 如果当前未运行,则在后台打开应用
  • 通过诸如MMWormhole
  • 之类的方式发送达尔文通知
  • openParentApplication:reply: - opens app in the background if not currently running
  • Darwin notifications through something like MMWormhole

从 Watch 扩展播放声音肯定是一个灰色地带.我的猜测是它会在 iOS 设备上播放,或者在使用设备进行测试时根本不播放.不过,我当然会建议不要采用这种方法.您的 Watch Extension 通常只会在很短的时间内打开.播放来自 iOS 应用的音频会是一个更好的主意,因为它可以保证继续运行.

Playing the sound from the Watch extension is certainly a gray area. My guess is that it will play on the iOS Device or not play at all when testing with devices. I would certainly advise against that approach though. Your Watch Extension will typically only be open for a VERY short period of time. It would be a much better idea to play the audio from the iOS app since that is guaranteed to continue running.