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)
self.audioPlayer.play()

      

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] = []
sounds.name = "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.

EDIT

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
        //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 still don't understand how to get ID3 information from mp3 files in swift. I have passed the AVAsset information to https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAsset_Class/index.html#//apple_ref/occ/cl/AVAsset but it is not clear yet how to use it.

+3


source to share


1 answer


I went through the AVAsset docs ( https://developer.apple.com/reference/avfoundation/avasset#//apple_ref/occ/cl/AVAsset ) and saw that it has a metadata property on it ( https://developer.apple.com / reference / avfoundation / avasset / 1386884-metadata ).

This is an AVMetadataItems array ( https://developer.apple.com/reference/avfoundation/avmetadataitem ) 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: https://github.com/philiphardy/ID3Edit

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

+1


source







All Articles