gpt4 book ai didi

swift - 在 WatchOS 上播放声音

转载 作者:行者123 更新时间:2023-11-30 10:54:29 25 4
gpt4 key购买 nike

我正在尝试使用 Xcode 为 Apple Watch 制作一个应用程序。这是一个非常简单的应用程序,每当播放按钮时就会播放声音,但我似乎找不到播放方式。

音频文件位于我的 WatchKit 扩展中,我尝试通过 WKAudioFilePlayer 对象播放它,但我不知道我的做法是否正确。

 override func awake(withContext context: Any?) {
super.awake(withContext: context)
kickPath = Bundle.main.path(forResource:"Kick", ofType: "mp3")!
kickUrl = URL(fileURLWithPath: kickPath!)
kickAsset = WKAudioFileAsset(url: kickUrl!)
kickItem = WKAudioFilePlayerItem(asset: kickAsset!)
kick = WKAudioFilePlayer(playerItem: kickItem!)
}

@IBAction func kickButton() {
switch kick.status {
case .readyToPlay:
kick.play()
print("sound")
case .failed:
print("failed")
case .unknown:
print("unknown")
}
}

音频无法播放,但我知道它位于正确的开关盒上,因为它打印出“声音”。

最佳答案

对我有用的是使用 AVFoundation 和 AVAudioPlayer 来播放声音,而不是我正在使用的 WKAudioFilePlayer。如果您连接了蓝牙设备,它会通过 watch 的扬声器和耳机发出声音。

关于swift - 在 WatchOS 上播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54116998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com