How to loop over multiple (audio / mp3) files in a swift directory? - Swift, iOS8 Soundboard AVPlayer

I am trying to build a sound board using Swift to play around and learn about swift / iOS8.

This is what I did in my TableViewController:

var soundPath = NSBundle.mainBundle().pathForResource("RandomSound", ofType: "m4a")
var soundURL = NSURL.fileURLWithPath(soundPath!)        
self.audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: nil)


This works to play audio as expected. Now, to have a few more sounds and display them in the tableView, I created an array of the Sound class that looks like this:

class Sound {
    var name: [String]
    var fileURL = NSURL()
    // more fields as required


and then using it like this:

var sounds: [Sound] = [] = "RandomSound"
sounds.fileURL = soundURL!


However, I want something to improve it. Let's say I have a bunch of mp3 files in one folder (e.g. 100).

In this case, I need some mechanism whereby I can just specify the directory and / or file type and then the remaining heavy lifting, like looking through each file in the directory and looking for the name / artist / and other information is extracted from the mp3 files ID3 tags (or something similar).

If not all data, at least filling in the name / url field and looping through all the files in the directory should be possible to automate , but I can't figure out what I should be looking at to achieve this.


This part allows me to scan all files in a directory of a given file type. In my case, I am looking for mp3. This is what I came 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
        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)
        //Instead it just makes sense to populate some array/dict containing all the sounfURLs = String(element)
        soundElement.fileURL = soundURL


class soundElement {
    var name = " "
    var fileURL = NSURL()


I still don't understand how to get ID3 information from mp3 files in swift. I have passed the AVAsset information to but it is not clear yet how to use it.


source to share

1 answer

I went through the AVAsset docs ( ) and saw that it has a metadata property on it ( / reference / avfoundation / avasset / 1386884-metadata ).

This is an AVMetadataItems array ( ) so that you can check items if they contain the metadata you are looking for (Keys usually matter AVMetadataiTunesMetadataKeySongName

in the keyspace AVMetadataKeySpaceiTunes

or AVMetadataID3MetadataKeyAlbumTitle

in AVMetadataKeySpaceID3


If you need a simpler solution, you can check out the ID3Edit library from this guy:

I would try the following:

  • Upload your songs as AVAsset
  • Iterating over the metadata array of each song
  • Print out the keys and values ​​of the elements to determine the ones that interest you.

Once you are familiar with the structure of the metadata, you can start looking for the ones you want.

Hope it helps



All Articles