I getting files from document directory with the help of NSFileManager
. I got an array of file names and I can pass them to another controller. Still I want to pass NSURL
on specific file from my UITableViewCell
as I pass names of these files. My files are mp3. Please help me.
var listOfMP3Files: Array<String!>? // for Cell data
func fetchFilesFromFolder() {
var fileManager = NSFileManager.defaultManager()
var folderPathURL = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)[0] as! NSURL
var documentsContent = fileManager.contentsOfDirectoryAtPath(folderPathURL.path!, error: nil)
println(documentsContent)
if var directoryURLs = fileManager.contentsOfDirectoryAtURL(folderPathURL, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles, error: nil) {
println(directoryURLs)
var mp3Files = directoryURLs.map(){ $0.lastPathComponent }.filter(){ $0.pathExtension == "mp3" }
listOfMP3Files = mp3Files
println(mp3Files)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var playerVC = (segue.destinationViewController as! UINavigationController).topViewController as! AVPlayerVC
var indexPath = tableView.indexPathForSelectedRow()
var objectForPass = listOfMP3Files![indexPath!.row] // default
if segue.identifier == "listenMusic" {
playerVC.object = objectForPass
}
}
UPDATE :
I updated my code and I can to get files of jpg type. But I don't know how to get mp3 files.
My updated code
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var playerVC = (segue.destinationViewController as! UINavigationController).topViewController as! PlayMusicViewController
var indexPath = tableView.indexPathForSelectedRow()
var objectForPass = listOfMP3Files![indexPath!.row] // default
//
var path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as? String
var getImage = path?.stringByAppendingPathComponent(objectForPass)
var image = UIImage(contentsOfFile: getImage!)
if segue.identifier == "listenMusic" {
playerVC.musicFile = objectForPass
playerVC.end = image
}
}
UPDATE2 I edited my code and I can to pass NSURL
on mp3 files and in another UIViewController
I can play them.
Below there is a edited code
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var playerVC = (segue.destinationViewController as! UINavigationController).topViewController as! PlayMusicViewController
var indexPath = tableView.indexPathForSelectedRow()
var objectForPass = listOfMP3Files![indexPath!.row] // default
//
/* var path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as? String
var getImage = path?.stringByAppendingPathComponent(objectForPass)
var image = UIImage(contentsOfFile: getImage!) */
//
var fileM = NSFileManager.defaultManager()
var wayToFile = fileM.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
var passMusicFileURL: NSURL?
if let documentPath: NSURL = wayToFile.first as? NSURL {
let musicFile = documentPath.URLByAppendingPathComponent(objectForPass)
println(musicFile)
passMusicFileURL = musicFile
}
if segue.identifier == "listenMusic" {
// playerVC.musicFile = objectForPass
playerVC.end = passMusicFileURL
}
}
Create the full file URL in a separate intermediate statement and you will find the problem.
If you do not have independent statements debugging is very hard.
folderPathURL.path
is incorrect.
First create the entire file path, then create f file URL from it.
Example:
let name = "aFileName"
var filePath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
println("\nfilePath: \(filePath)")
filePath = filePath.stringByAppendingPathComponent(name)
println("\nfilePath: \(filePath)")
var filePathURL = NSURL.fileURLWithPath(filePath)
println("\nfilePathURL: \(filePathURL!)")
Output:
filePath: /Users/zaph/Library/Developer/CoreSimulator/Devices/264B31F6-3A28-4C75-9205-3558BBF54DED/data/Containers/Data/Application/0048403F-C8DE-4EE7-83CF-6AE7CCBAF090/Documents
filePath: /Users/zaph/Library/Developer/CoreSimulator/Devices/264B31F6-3A28-4C75-9205-3558BBF54DED/data/Containers/Data/Application/0048403F-C8DE-4EE7-83CF-6AE7CCBAF090/Documents/aFileName
filePathURL: file:///Users/zaph/Library/Developer/CoreSimulator/Devices/264B31F6-3A28-4C75-9205-3558BBF54DED/data/Containers/Data/Application/0048403F-C8DE-4EE7-83CF-6AE7CCBAF090/Documents/aFileName
With the intermediate statements each step can be validated.
I found a solution for my question
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var playerVC = (segue.destinationViewController as! UINavigationController).topViewController as! PlayMusicViewController
var indexPath = tableView.indexPathForSelectedRow()
var objectForPass = listOfMP3Files![indexPath!.row] // default
//
/* var path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as? String
var getImage = path?.stringByAppendingPathComponent(objectForPass)
var image = UIImage(contentsOfFile: getImage!) */
//
var fileM = NSFileManager.defaultManager()
var wayToFile = fileM.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
var passMusicFileURL: NSURL?
if let documentPath: NSURL = wayToFile.first as? NSURL {
let musicFile = documentPath.URLByAppendingPathComponent(objectForPass)
println(musicFile)
passMusicFileURL = musicFile
}
if segue.identifier == "listenMusic" {
// playerVC.musicFile = objectForPass
playerVC.end = passMusicFileURL
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.