Я могу легко загрузить .mp3
и .aiff
аудиофайлы, используя метод типа RealityKit load(contentsOf: withName: inputMode: loadingStrategy: shouldLoop:)
.
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()
:
Bundle.main.url()
отлично работает для загрузки (contentsOf:).
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(named: in: inputMode: loadingStrategy: shouldLoop:)
.
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()
методом:
Bundle.main.path()
не работает для нагрузки (named:).
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)
}
}
Чего мне не хватает? Как я могу это сделать?