Pages

Extracting data like thumbnail images , Artist Name etc. from Audio file

   As a iOS developer if you are to design your custom media player then how will you get the information like thumbnail images, artist name, singer name etc. this is what I will show you in this post. But before that you should know about some classes && functions because you will be needed those classes && their functions in the code.

AVAsset : AVAsset is basically an abstract class that represents media such as Audio files or video files. And each asset contains a collection (big/small) of tracks that will be processed together. An AVAsset object defines different properties of the tracks. You instantiate asset using AVURLAsset class that represents media like mp3 files, mp4 files avi fils etc.
Now create an XCode project with single view application template && choose language as Swift. Next step is to drag a "mp3" format file to your project navigator && also connect an outlet to your imageView that you drag onto your viewController. Scene Next step is to add the following bit of code to the viewDidLoad()  method of your ViewController.Swift file.

 @IBOutlet var imageView1: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
                    //getting the path of the mp3 file
        var filePath = NSBundle.mainBundle().pathForResource("Dil", ofType: "mp3")
                    //transforming it to url
        var fileUrl = NSURL(fileURLWithPath: filePath!)
                    //instanciating asset with url associated file
        var asset = AVAsset.assetWithURL(fileUrl) as AVAsset
        
                    //using the asset property to get the metadata of file
        for metaDataItems in asset.commonMetadata {
                    //getting the title of the song
            if metaDataItems.commonKey == "title" {
                let titleData = metaDataItems.value as NSString
                var title = titleData.substringToIndex(9)
                var singerData = titleData.substringFromIndex(12)
                var singer = (singerData as NSString).substringToIndex(13)
                println("title ---> \(title)")
                println("singer ---> \(singer)")
            }
                    //getting the "Artist of the mp3 file"
            if metaDataItems.commonKey == "artist" {
                let artistData = metaDataItems.value as NSString
                println("artist ---> \(artistData)")
            }
                    //getting the thumbnail image associated with file
            if metaDataItems.commonKey == "artwork" {
                let imageData = metaDataItems.value as NSData
                var image2: UIImage = UIImage(data: imageData)!
                imageView1.image = image2
            }
        }
    }

Here in this code all you are doing is getting the path of the mp3 file that you dragged in the project. Then transforming the path to the NSURL && then creating the object of the AVAsset class. After the you loop through the metadata information about the file && one by one getting the properties that we needed.
Noted that how you extracted the artist name && singer name && track name  from the huge information associated with file, by using the string handling functions. Output of your Console is:



I hope guys that was useful for your media player.