问题
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