问题描述
我可以使用RealityKit的 load(contentsOf:withName:inputMode:loadingStrategy:shouldLoop:)
轻松加载 .mp3
和 .aiff
音频文件类型方法.
I can easily load .mp3
and .aiff
audio files using RealityKit's load(contentsOf: withName: inputMode: loadingStrategy: shouldLoop:)
type method.
static func load(contentsOf url: URL,
withName resourceName: String? = nil,
inputMode: AudioResource.InputMode = .spatial,
loadingStrategy: AudioFileResource.LoadingStrategy = .preload,
shouldLoop: Bool = false) throws -> AudioFileResource
这是带有 Bundle.main.url()
方法的代码:
import RealityKit
import ARKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
let entity = Entity()
let anchor = AnchorEntity()
var audioController: AudioPlaybackController? = nil
override func viewDidLoad() {
super.viewDidLoad()
self.loadAudio()
}
}
extension ViewController {
fileprivate func loadAudio() {
let audioURL: URL = Bundle.main.url(forResource: "MonoAudio",
withExtension: "mp3")!
do {
let audioResource = try AudioFileResource.load(contentsOf: audioURL)
self.audioController = entity.prepareAudio(audioResource)
self.audioController?.fade(to: .infinity, duration: 5)
self.audioController?.speed = 2.0
self.audioController?.gain = 30
self.audioController?.play()
} catch {
print("Get Error while loading audio file...")
}
self.anchor.addChild(entity)
self.arView.scene.anchors.append(anchor)
}
}
但是我不能使用 load(命名为:inputMode:loadingStrategy:shouldLoop:)
类型方法来加载音频文件.
But I can't load audio file using load(named: in: inputMode: loadingStrategy: shouldLoop:)
type method.
static func load(named name: String,
in bundle: Bundle? = nil,
inputMode: AudioResource.InputMode = .spatial,
loadingStrategy: AudioFileResource.LoadingStrategy = .preload,
shouldLoop: Bool = false) throws -> AudioFileResource
这是带有 Bundle.main.path()
方法的代码:
extension ViewController {
fileprivate func loadAudio() {
let audioPath: String = Bundle.main.path(forResource: "MonoAudio",
ofType: "mp3")!
do {
let audioResource = try AudioFileResource.load(named: audioPath)
self.audioController = entity.prepareAudio(audioResource)
self.audioController?.fade(to: .infinity, duration: 5)
self.audioController?.speed = 2.0
self.audioController?.gain = 30
self.audioController?.play()
} catch {
print("Get Error while loading audio file...")
}
self.anchor.addChild(entity)
self.arView.scene.anchors.append(anchor)
}
}
推荐答案
此处无需使用 Bundle.main.path(forResource:ofType:)
方法.我需要做的就是为字符串参数 named
分配一个文件名.看起来是这样的:
There's no need to use a Bundle.main.path(forResource:ofType:)
method here. All I need to do is to assign a file name to string argument named
. Here's how it looks like:
let audioResource = try AudioFileResource.load(named: "MonoAudio.mp3",
in: nil,
inputMode: .spatial,
loadingStrategy: .preload,
shouldLoop: true)
self.audioController = entity.prepareAudio(audioResource)
self.audioController?.play()
这篇关于无法使用"AudioFileResource.load(named :)"加载音频;类型方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!