Displaying Artwork for .MP3 file

Unconquered82 picture Unconquered82 · May 14, 2015 · Viewed 9.2k times · Source

I am trying to currently display the album artwork for a locally stored .MP3 track in an ImageView. Does anyone know how to fetch this artwork in Swift in order to accomplish this?

I have found this solution (iOS AVFoundation: How do I fetch artwork from an mp3 file?) but the code is written in Objective C. I simply want to grab the image embedded in my MP3 and display it in my ImageView.

I've looked at the API documentation for the MPMediaItemArtwork and found an example that also accomplishes what I am trying to accomplish in Objective C as well here(http://www.codeitive.com/0zHjkUjUWX/not-able-to-get-the-uiimage-from-mpmediaitempropertyartwork.html) but cannot come up with a solution. My code is as follows:

import UIKit
import AVFoundation
import MediaPlayer

class ViewController: UIViewController {
let audioPath:NSURL! = NSBundle.mainBundle().URLForResource("SippinOnFire", withExtension: "mp3")

@IBOutlet var artistImage: UIImageView!
@IBOutlet var trackLabel: UILabel!
@IBOutlet var artistLabel: UILabel!
@IBOutlet var sliderValue: UISlider!
var player:AVAudioPlayer = AVAudioPlayer()


@IBAction func play(sender: AnyObject) {


    let audioInfo = MPNowPlayingInfoCenter.defaultCenter()
println(audioInfo)


    player.play()
    //println("Playing \(audioPath)")


    let playerItem = AVPlayerItem(URL: audioPath)
    let metadataList = playerItem.asset.metadata as! [AVMetadataItem]


    for item in metadataList {
        if let stringValue = item.value {
           println(item.commonKey)
            if item.commonKey == "title" {
                trackLabel.text = stringValue as? String
            }
            if item.commonKey  == "artist" {
                artistLabel.text = stringValue as? String
            }
            if item.commonKey  == "artwork" {
                if let audioImage = UIImage(data: item.value as! NSData) {
                    let audioArtwork = MPMediaItemArtwork(image: audioImage)
                    println(audioImage.description)
                }
           }


        }
    }
}
@IBAction func pause(sender: AnyObject) {

    player.pause()
}
@IBAction func stop(sender: AnyObject) {

    player.stop()
    player.currentTime = 0;
}
@IBAction func sliderChanged(sender: AnyObject) {

    player.volume = sliderValue.value

}

override func viewDidLoad() {
    super.viewDidLoad()

             var error:NSError? = nil

    player = AVAudioPlayer(contentsOfURL: audioPath!, error: &error)

    player.volume = 0.5;

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

Here is a screen shot of my sample .mp3 file. As you can see there is indeed album artwork that is both visible in the "get info" section of Finder. I've also opened the .mp3 in my iTunes to make sure and have confirmed there is artwork in the "get info" section of it there as well as under the "artwork" tab.

However, when trying to use the commonKey to assign the image to my imageView I find that there is no commonKey for "artwork".

sample MP3 data

Thanks

Answer

Hugo Alonso picture Hugo Alonso · May 14, 2015

Change your snippet of code into this (I already tested it):

I added println lines commented in places of interest, Feel free to uncomment in order to see what is happening.

   for item in metadataList {
        if item.commonKey == nil{
            continue
        }

        if let key = item.commonKey, let value = item.value {
            //println(key)
            //println(value)
            if key == "title" {
                trackLabel.text = value as? String
            }
            if key  == "artist" {
                artistLabel.text = value as? String
            }
            if key == "artwork" {
                if let audioImage = UIImage(data: value as! NSData) {
                  //println(audioImage.description)
                    artistImage.image = audioImage
                }
            }
        }
    }

UPDATE: A bit of clean up of this code

for item in metadataList {

    guard let key = item.commonKey, let value = item.value else{
        continue
    }

   switch key {
    case "title" : trackLabel.text = value as? String
    case "artist": artistLabel.text = value as? String
    case "artwork" where value is NSData : artistImage.image = UIImage(data: value as! NSData)
    default:
      continue
   }
}

UPDATE: For Swift 4

for item in metadataList {

    guard let key = item.commonKey?.rawValue, let value = item.value else{
        continue
    }

   switch key {
    case "title" : trackLabel.text = value as? String
    case "artist": artistLabel.text = value as? String
    case "artwork" where value is Data : artistImage.image = UIImage(data: value as! Data)
    default:
      continue
   }
}