我正在学习如何为 vision pro 进行开发,并且我正在尝试制作一个简单的气泡,当与它交互时会发出砰砰的声音。
我已经创建了模型、查看了所有内容,但是当我点击或触摸气泡时,我无法播放音频。
struct ImmersiveView: View {
@State var predicate = QueryPredicate<Entity>.has(ModelComponent.self)
@State private var timer: Timer?
@State private var audioController: AudioPlaybackController?
var body: some View {
RealityView { content in
// Add the initial RealityKit content
if let immersiveContentEntity = try? await Entity(named: "Bubble", in: realityKitContentBundle) {
content.add(immersiveContentEntity)
// Put skybox here. See example in World project available at
// https://developer.apple.com/
}
}.gesture(SpatialTapGesture().targetedToEntity(where: predicate).onEnded({
value in
let entity = value.entity
let spatialAudio = entity.findEntity(named: "SpatialAudio")
guard let resource = try? AudioFileResource.load(named: "/Bubble/bubble_sound_43207.mp3", from: "Bubble.usda", in: realityKitContentBundle) else {
fatalError("Unable to find bubble audio file.")
}
audioController = (spatialAudio?.prepareAudio(resource))!
audioController?.play()
var material = entity.components[ModelComponent.self]?.materials.first as! ShaderGraphMaterial
let frameRate: TimeInterval = 1.0/60.0
let duration: TimeInterval = 0.25
let targetValue: Float = 1
let totalFrames = Int(duration / frameRate)
var currentFrame = 0
var popValue: Float = 0
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: frameRate, repeats: true, block: { timer in
currentFrame += 1
let progress: Float = Float(currentFrame) / Float(totalFrames)
popValue = progress * targetValue
do {
try material.setParameter(name: "Pop", value: .float(popValue))
entity.components[ModelComponent.self]?.materials = [material]
}
catch {
print(error.localizedDescription)
}
if currentFrame >= totalFrames {
timer.invalidate()
entity.removeFromParent()
}
})
}))
}
}
我最大的猜测是它找不到音频文件,但我已经尝试了我能想到的所有可能的路径。
我想不出如何共享文件结构,但如果需要的话我可以共享。
请让我知道我搞砸了什么。哈哈。