I'm currently downloading audio files from Firebase saving them locally and listing them in a tableview. Im getting the local directory like this:
file:///var/mobile/Containers/Data/Application/6C72433F-4DF0-4976-BBDE-36F6B5C23E6D/Documents/nJNJwVNMrmUGj9uszManQmMOZf52_NO:1.m4a
When i try to play an audio file with AVAudioPLayer i get the error:
Error Domain=NSOSStatusErrorDomain Code=2003334207 "(null)"
I've been stuck on this for three days and would really appreciate someone pointing me in the right direction. If any further code is needed to help solve this let me know. Thanks.
This is my code for playing the audio:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//Access the cell tapped on and the path to the recording the user wants to listen to
let path = findDirectory(filename:userID + "_NO:" + String(recordingNumber)).appendingPathComponent("\(indexPath.row + 1).m4a")
do {
self.audioPlayer = try AVAudioPlayer(contentsOf: path)
self.audioPlayer.play()
} catch let error as NSError {
print(error.description)
}
}
My code for finding the directory is here:
func findDirectory(filename:String) -> URL {
let fileManager = FileManager.default
let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
let docDirectory = urls[0] as URL
let soundURL = docDirectory.appendingPathComponent("\(filename).m4a")
print(soundURL)
return soundURL
}
If the file you download is saved as nJNJwVNMrmUGj9uszManQmMOZf52_NO:1.m4a
why are you appending on top of it through: .appendingPathComponent("\(indexPath.row + 1).m4a")
this value <row_value + 1>.m4a
in your tableview?
Its like now you are trying to find a file saved as: nJNJwVNMrmUGj9uszManQmMOZf52_NO:1.m4a<row_value + 1>.m4a
.
Here <row_value + 1>
is a placeholder I used in this answer to show you the meaning but in real code will be replaced by either a value like 1
, 2
,...
To explain it better let's analyse:
let path = findDirectory(filename:userID + "_NO:" + String(recordingNumber)).appendingPathComponent("\(indexPath.row + 1).m4a")
userID + "_NO:"
will produce something like: nJNJwVNMrmUGj9uszManQmMOZf52_NO:
String(recordingNumber)
will produce something like: 1
(for example)findDirectory(filename: ...)
with the above will give you back nJNJwVNMrmUGj9uszManQmMOZf52_NO:1.m4a
as of the line let soundURL = docDirectory.appendingPathComponent("\(filename).m4a")
.appendingPathComponent("\(indexPath.row + 1).m4a")
which appends more to your path that looked already completedYou'd likely want to modify the logic to better match how you expect the path to work with your tableview, so that you compose the right path for your file (maybe that means in your case removing the extra appending you do in step 4)