You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ARSCNView场景中SCNAudioPlayer音频播放异常问题排查

ARKit + SceneKit 音频播放问题

我正在用ARKit和SceneKit开发AR应用,在ViewController类中遇到了音频播放问题。我在ARSCNView里有一个可旋转移动的立方体SCNNode,想要通过SCNAudioPlayer为该节点添加音频播放功能。

已设置音频源并尝试添加到节点,但场景运行时无音频输出。确认过音频文件sample_audio.mp3路径正确,SCNAudioSource的初始化和加载也没有问题。

简化后的代码

import UIKit
import ARKit
import SnapKit
import SceneKit

class ViewController: UIViewController {

    // MARK: - Property
    
    // MARK: - execute
    private func execute() {
        action_example1()
    }
    
    // MARK: - UIComponents
    private lazy var arSceneView: ARSCNView = {
        $0.delegate = self
        $0.showsStatistics = true
        return $0
    }(ARSCNView())
}

extension ViewController {
    func action_example1() {
        let scene = SCNScene()
        arSceneView.scene = scene

        let boxGeometry = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0.1)
        
        var boxNode = SCNNode()
        boxNode = SCNNode(geometry: boxGeometry)
        boxNode.position = SCNVector3(0, 0, -5)
        
        let material = SCNMaterial()
//        material.diffuse.contents = UIImage(resource: .pyramidFull)
        
        boxNode.geometry?.firstMaterial = material
        scene.rootNode.addChildNode(boxNode)

        let rotateAction = SCNAction.rotateBy(x: 0, y: .pi, z: 0, duration: 2)
        let moveAction = SCNAction.move(to: SCNVector3(0, 0, +10), duration: 2)
        let sequenceAction = SCNAction.sequence([rotateAction, moveAction])
        let repeatAction = SCNAction.repeatForever(sequenceAction)
        
        
        let audioSource = SCNAudioSource(fileNamed: "sample_audio.mp3")!
        audioSource.loops = true
        boxNode.runAction(SCNAction.playAudio(audioSource, waitForCompletion: false))
        audioSource.load()
        
        
        boxNode.runAction(repeatAction)
    }
}

extension ViewController: ARSCNViewDelegate {
    
    // MARK: - ARSCNViewDelegate
    
/*
    // Override to create and configure nodes for anchors added to the view's session.
    func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
        print(#function)
        let node = SCNNode()
        
        return node
    }
*/
    
    func session(_ session: ARSession, didFailWithError error: Error) {
        // Present an error message to the user
        print(#function)
        
    }
    
    func sessionWasInterrupted(_ session: ARSession) {
        // Inform the user that the session has been interrupted, for example, by presenting an overlay
        print(#function)
    }
    
    func sessionInterruptionEnded(_ session: ARSession) {
        // Reset tracking and/or remove existing anchors if consistent tracking is required
        print(#function)
    }
}

已尝试的解决步骤

  • 创建SCNAudioSource实例并设置loops = true
  • 调用load()方法确保音频源加载完成
  • 通过playAudio动作添加音频播放逻辑

预期音频会随SCNNode的动画持续播放,但实际无音频输出,想问有没有遗漏的配置或额外操作需要执行?


问题排查与解决方案

  1. 调整音频加载与播放的顺序
    你的代码中先执行了播放动作,再调用audioSource.load(),这会导致音频还未加载完成就触发播放,自然没有声音。需要先加载音频源,再执行播放动作:

    let audioSource = SCNAudioSource(fileNamed: "sample_audio.mp3")!
    audioSource.loops = true
    audioSource.load() // 先完成加载
    boxNode.runAction(SCNAction.playAudio(audioSource, waitForCompletion: false)) // 再执行播放
    
  2. 配置AVAudioSession
    AR应用默认可能未激活音频会话,需要手动配置以确保音频正常输出。在action_example1方法开头添加以下代码,记得先导入AVFoundation框架:

    import AVFoundation
    
    do {
        try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
        try AVAudioSession.sharedInstance().activate()
    } catch {
        print("音频会话配置失败: \(error)")
    }
    
  3. 改用SCNAudioPlayer直接绑定节点
    除了SCNAction的方式,直接给节点添加SCNAudioPlayer组件会更稳定,音频会自动跟随节点生命周期播放:

    let audioSource = SCNAudioSource(fileNamed: "sample_audio.mp3")!
    audioSource.loops = true
    audioSource.load()
    let audioPlayer = SCNAudioPlayer(source: audioSource)
    boxNode.addAudioPlayer(audioPlayer)
    
  4. 关闭音频空间化(可选)
    SceneKit默认会根据节点与相机的距离调整音量,如果节点移动后距离过远,可能导致音量衰减为0。可以关闭空间化效果强制播放:

    audioSource.isSpatialized = false
    

内容的提问来源于stack exchange,提问作者StrongMan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 11:53:20