gpt4 book ai didi

swift - 播放文档目录中的 m4a 文件

转载 作者:行者123 更新时间:2023-11-30 11:50:03 29 4
gpt4 key购买 nike

我想播放 m4a 文件,但我在控制台中收到错误,并且该文件无法播放。

控制台打印:

The operation couldn’t be completed. (OSStatus error 2003334207.)

let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
print("File Location: ",url.path)

if(FileManager.default.fileExists(atPath: url.path)) {
do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
guard let player = audioPlayer else { return }

player.prepareToPlay()
} catch let error {
print(error.localizedDescription)
}

} else {
print("file not found")
}

我尝试过这样的网址:

let NoteUrl = Bundle.main.url(forResource: "Voice Note", withExtension: "m4a")

但我无法让它播放。有人可以帮我按文件名播放 m4a 文件吗?任何帮助将不胜感激。

最佳答案

您正在将文档文件夹传递到音频播放器。您的代码需要将特定文件名附加到 Documents 文件夹。

let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let url = documentsURL.appendingPathComponent("Voice Note.m4a")

关于swift - 播放文档目录中的 m4a 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48429430/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com