问题描述
Apple Watch 可以使用 AVFoundation
吗?更具体地说,AVAudioPlayer
和 AVAudioRecorder
可以工作吗?
Can the Apple Watch use AVFoundation
? More specifically, can AVAudioPlayer
and AVAudioRecorder
work?
我正在尝试制作一个应用程序,让您可以将声音录制到 Apple Watch 并使用音频播放器播放.谢谢
I am trying to make an app that lets you record a sound to the Apple Watch and have it play it back using the audioplayer. Thanks
推荐答案
尝试使用 WKAudioFilePlayerItem
吗?使用这个类播放音频,需要声明3个变量:
Try using WKAudioFilePlayerItem
?To use this class to play audio, you need to declare 3 variables:
var audioFile: WKAudioFileAsset!
var audioItem: WKAudioFilePlayerItem!
var audioPlayer: WKAudioFilePlayer!
他们在程序中扮演着不同的角色.audioFile
用于定义 NSURL
.例如,如果您有名为bicycle.mp3"的文件,您可以像这样定义 URL 路径:
They have different role inside the program. For audioFile
is used to define NSURL
. For example, if you have file called "bicycle.mp3", you can define the URL path like this :
let soundPath = NSBundle.mainBundle().pathForResource("bicycle", ofType: "mp3")
print("load file")
当你有一个 soundpath
时,你可以在 audioFile 中把它变成一个 NSURL:
when you have a soundpath
, you make it a NSURL inside audioFile:
audioFile = WKAudioFileAsset.init(URL: soundPathURL)
当你有一个 audioFile
时,你把它放在 audioPlayerItem
When you have a audioFile
, you put it inside the audioPlayerItem
audioItem = WKAudioFilePlayerItem.init(asset: audioFile)
当你有 audioItem
时,你把它放在 audioPlayer
When you have audioItem
, you put it inside the audioPlayer
`audioPlayer = WKAudioFilePlayer(playerItem: audioItem)`
最后,你可以把代码audioPlayer.play()
放在你想播放声音的地方
Finally, you can put the code audioPlayer.play()
where you want to play the sound
享受吧!
这篇关于Apple Watch 可以使用 AVFoundation 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!