I'm writing a podcast app and I want the user to play the local file they've downloaded if it exists (rather than stream it). The streaming works fine, but if the local file is found, it's not played by AVPlayer (this is actually not true for one out of about 5 files for reasons beyond me -- tapping on one podcast will play locally (I'm in airplane mode to verify).
I believe this is the Swift equivalent of this question, but I haven't been able to implement that solution in Swift.
Here's the code:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var theURL = posts.objectAtIndex(indexPath.row).valueForKey("enclosure") as? String
var theMp3Filename = theURL!.lastPathComponent
for i in downloadedList {
if i as! String == theMp3Filename {
// If we've d/l'ed the mp3, insert the local path
theURL = documentsDirectory().stringByAppendingPathComponent(theMp3Filename)
var path: NSURL = NSURL.fileURLWithPath(theURL!)!
player = AVPlayer(URL: path)
player.play()
} else {
let podcastEpisode = AVPlayerItem(URL: NSURL(string: theURL!))
player = AVPlayer(playerItem: podcastEpisode)
player.play()
}
}
}
downloadedList
is just an array I generate with the downloaded filenames. I am getting into the local condition, and the path looks right, but AVPlayer usually isn't working.
Any guidance would be appreciated.