Displaying Artwork for .MP3 file

梦想与她 提交于 2019-11-27 20:19:00
Hugo Alonso

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

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

Try this:

It appears that sometimes iOS 8 returns nil at first attempt of obtaining this info:

if let audioCenter = MPNowPlayingInfoCenter.defaultCenter(){
        if let audioInfo = audioCenter.nowPlayingInfo{
            if let artwork = audioInfo[MPMediaItemPropertyArtwork] as? MPMediaItemArtwork
            {
                var image: UIImage? = artwork.imageWithSize(artwork.bounds.size)

                if image == nil {
                    image = artwork.imageWithSize(artwork.bounds.size);
                }

                if image != nil{
                    println("image loaded")
                }
            }
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!