Displaying Artwork for .MP3 file

edit/update Swift 4 or later:

import MediaPlayer


var nowPlayingInfo: [String: Any] = [:]
let playerItem = AVPlayerItem(url: url)
let metadataList = playerItem.asset.metadata

for item in metadataList {
    switch item.commonKey {
    case .commonKeyTitle?:
        nowPlayingInfo[MPMediaItemPropertyTitle] = item.stringValue ?? ""
    case .commonKeyType?:
        nowPlayingInfo[MPMediaItemPropertyGenre] = item.stringValue ?? ""
    case .commonKeyAlbumName?:
        nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = item.stringValue ?? ""
    case .commonKeyArtist?:
        nowPlayingInfo[MPMediaItemPropertyArtist] = item.stringValue ?? ""
    case .commonKeyArtwork?:
        if let data = item.dataValue,
            let image = UIImage(data: data) {
            nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { _ in image }
        }
    case .none: break
    default: break
    }
}

let audioInfo = MPNowPlayingInfoCenter.default()
audioInfo.nowPlayingInfo = nowPlayingInfo

Note: You will have to invoke beginReceivingRemoteControlEvents() otherwise it will not work on the actual device. You will also need to set your app Background Modes (Audio and AirPlay) and set your AVAudioSession category to AVAudioSessionCategoryPlayback and set it active:

do {
    try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])
    print("Playback OK")
    try AVAudioSession.sharedInstance().setActive(true)
    print("Session is Active")
} catch {
    print(error)
}

enter image description here


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
   }
}