Displaying Artwork for .MP3 file

前端 未结 3 783
星月不相逢
星月不相逢 2020-12-05 08:56

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 accomplis

相关标签:
3条回答
  • 2020-12-05 09:12

    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
       }
    }
    
    0 讨论(0)
  • 2020-12-05 09:24

    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

    0 讨论(0)
  • 2020-12-05 09:24

    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")
                    }
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题