Apple Watch可以使用AVFoundation吗? [英] Can the Apple Watch use AVFoundation?
问题描述
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
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吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!