iOS - 如何使用 swift 从共享文件夹中获取音乐文件
iOS - how to get music file from shared folder with use swift
我用 swift 做了一个音乐播放器。我希望用户通过 iTunes 中的共享文件夹添加音乐文件。我创建了共享文件夹。如何从共享文件夹加载文件到 UITableViewCell
然后管理它们?
我写了下面的代码。对我有用
func exportFilesFromFolder() {
var folder = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).first as! NSURL
var allFiles = fileManager.contentsOfDirectoryAtURL(folder, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles, error: nil)!
var filterArray = allFiles.map(){ [=10=].lastPathComponent }.filter(){ [=10=].pathExtension == "mp3" } as [String]
// println("filter array \(allFiles[3])")
var superURL: NSURL!
for itemInFilterArray in filterArray {
var direct = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
if var url: NSURL = direct.first as? NSURL {
superURL = url.URLByAppendingPathComponent(itemInFilterArray)
}
var playerItem = AVPlayerItem(URL: superURL)
var equalazerString: String!
var commonMetaData = playerItem.asset.commonMetadata as! [AVMetadataItem]
for item in commonMetaData {
if item.commonKey == "title" {
equalazerString = item.stringValue
if equalazerString == currentSongString {
localArrayNames.append(superURL)
} else {
// println("it is not equals")
}
}
}
}
}
我用 swift 做了一个音乐播放器。我希望用户通过 iTunes 中的共享文件夹添加音乐文件。我创建了共享文件夹。如何从共享文件夹加载文件到 UITableViewCell
然后管理它们?
我写了下面的代码。对我有用
func exportFilesFromFolder() {
var folder = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).first as! NSURL
var allFiles = fileManager.contentsOfDirectoryAtURL(folder, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles, error: nil)!
var filterArray = allFiles.map(){ [=10=].lastPathComponent }.filter(){ [=10=].pathExtension == "mp3" } as [String]
// println("filter array \(allFiles[3])")
var superURL: NSURL!
for itemInFilterArray in filterArray {
var direct = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
if var url: NSURL = direct.first as? NSURL {
superURL = url.URLByAppendingPathComponent(itemInFilterArray)
}
var playerItem = AVPlayerItem(URL: superURL)
var equalazerString: String!
var commonMetaData = playerItem.asset.commonMetadata as! [AVMetadataItem]
for item in commonMetaData {
if item.commonKey == "title" {
equalazerString = item.stringValue
if equalazerString == currentSongString {
localArrayNames.append(superURL)
} else {
// println("it is not equals")
}
}
}
}
}