Reputation: 750
I am trying to create a soundboard using Swift to fiddle around and learn about swift/iOS8.
This is what I have done in my tableViewController as of now:
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:
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).
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).
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.
EDIT
This part allows me scan all files in a directory of a given filetype(s). In my case I am looking for 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. 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.
Upvotes: 3
Views: 2068
Reputation: 6429
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).
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
.
If you want an easier solution, you could check out the ID3Edit library from this guy: 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
Upvotes: 1