Play m4a file from document Directory

北城余情 提交于 2019-12-11 16:48:21

问题


I would to play an m4a file but I'm getting an error in the console and the file won't play.

Console prints:

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")
}

I tried URL like this:

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

But I couldn't get it to play. Can someone help me to play m4a file by file's name? Any help will be appreciated.


回答1:


You are passing the Documents folder to the audio player. Your code needs to append the specific filename to the Documents folder.

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


来源:https://stackoverflow.com/questions/48429430/play-m4a-file-from-document-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!