[英]How to loop over multiple (audio/mp3) files in a directory with swift? - Swift, iOS8 Soundboard AVPlayer
I am trying to create a soundboard using Swift to fiddle around and learn about swift/iOS8. 我正在尝试使用Swift创建一个音板来摆弄并学习swift / iOS8。
This is what I have done in my tableViewController as of now: 这就是我现在在tableViewController中所做的事情:
var soundPath = NSBundle.mainBundle().pathForResource("RandomSound", ofType: "m4a")
var soundURL = NSURL.fileURLWithPath(soundPath!)
self.audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: nil)
self.audioPlayer.play()
This works to play the sound as expected. 这可以按预期播放声音。 Now in order to have a few more sounds and display them in tableView, I created an array of Sound class which looks like this: 现在为了有更多的声音并在tableView中显示它们,我创建了一个Sound类数组,如下所示:
class Sound {
var name: [String]
var fileURL = NSURL()
// more fields as required
}
and then using it like this: 然后像这样使用它:
var sounds: [Sound] = []
sounds.name = "RandomSound"
sounds.fileURL = soundURL!
However, I want something to make it to scale better . 但是, 我想要一些东西让它更好地扩展 。 Lets say I have a bunch of mp3 files in one folder (for example 100). 可以说我在一个文件夹中有一堆mp3文件(例如100)。
In this case I would like some mechanism, with which, I can simply point the directory and/or filetype and then the remaining heavy lifting like going through each file in the directory and finding the name/artist/and other information would be extracted from mp3 file ID3 tags (or something similar). 在这种情况下, 我想要一些机制,使用它,我可以简单地指向目录和/或文件类型,然后剩下的繁重工作,如通过目录中的每个文件,并找到名称/艺术家/和其他信息将从中提取mp3文件ID3标签(或类似的东西)。
If not all details, at least populating the name/URL field and looping over all files in a directory should be possible to automate , but I'm unable to understand what I should be looking at to achieve this. 如果不是所有细节, 至少填充名称/ URL字段并循环遍历目录中的所有文件应该可以自动化 ,但我无法理解我应该看到什么来实现这一点。
EDIT 编辑
This part allows me scan all files in a directory of a given filetype(s). 这部分允许我扫描给定文件类型的目录中的所有文件。 In my case I am looking for mp3. 在我的情况下,我正在寻找MP3。 This is what I've come up with: 这就是我想出来的:
soundArray: [soundElement] = []
let fileManager = NSFileManager.defaultManager()
let directoryPath = "/path/to/directory/containing/my/sound/files"
let enumerator:NSDirectoryEnumerator = fileManager.enumeratorAtPath(directoryPath)!
while let element = enumerator.nextObject() as? String {
if element.hasSuffix("mp3") { // checks the extension
// DO my sound path stuff here
//println(element)
let soundURL = NSURL.fileURLWithPath(directoryPath + element)
//Of course I'll not be playing them directly here, (I don't see why) but if I needed to ...
//self.audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: nil)
//self.audioPlayer.play()
//Instead it just makes sense to populate some array/dict containing all the sounfURLs
soundElement.name = String(element)
soundElement.fileURL = soundURL
self.soundArray.append(soundElement)
}
}
class soundElement {
var name = " "
var fileURL = NSURL()
}
I am still not clear on how to get ID3 information from mp3 files in swift. 我还不清楚如何从swift中的mp3文件中获取ID3信息。 I have referred the AVAsset information at https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAsset_Class/index.html#//apple_ref/occ/cl/AVAsset but it is not clear how to use it yet. 我在https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAsset_Class/index.html#//apple_ref/occ/cl/AVAsset上提到了AVAsset信息,但目前尚不清楚如何使用还没完成。
I looked at the AVAsset ( https://developer.apple.com/reference/avfoundation/avasset#//apple_ref/occ/cl/AVAsset ) docs and saw that there is a metadata property on it ( https://developer.apple.com/reference/avfoundation/avasset/1386884-metadata ). 我查看了AVAsset( https://developer.apple.com/reference/avfoundation/avasset#//apple_ref/occ/cl/AVAsset )文档,发现它上面有一个元数据属性( https:// developer。 apple.com/reference/avfoundation/avasset/1386884-metadata )。
It is an array of AVMetadataItems ( https://developer.apple.com/reference/avfoundation/avmetadataitem ) so you could check the items if they contain the metadata you are looking for (Keys are usually something like AVMetadataiTunesMetadataKeySongName
in the key space AVMetadataKeySpaceiTunes
or AVMetadataID3MetadataKeyAlbumTitle
in the AVMetadataKeySpaceID3
. 这是AVMetadataItems数组( https://developer.apple.com/reference/avfoundation/avmetadataitem ),所以你可以检查的项目,如果它们包含你正在寻找的元数据(键通常是像AVMetadataiTunesMetadataKeySongName
密钥空间AVMetadataKeySpaceiTunes
或AVMetadataID3MetadataKeyAlbumTitle
中的AVMetadataKeySpaceID3
。
If you want an easier solution, you could check out the ID3Edit library from this guy: https://github.com/philiphardy/ID3Edit 如果你想要一个更简单的解决方案,你可以查看这个人的ID3Edit库: https : //github.com/philiphardy/ID3Edit
I'd try the following: 我试试以下内容:
Once you are familiar with the structure of the metadata, you can start retrieving the ones you want. 熟悉元数据的结构后,即可开始检索所需的元数据。
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.