Work mapping for .MP3 file

I am trying to currently display album art for a locally saved .MP3 track in ImageView. Does anyone know how to get this work in Swift to do this?

I found this solution ( iOS AVFoundation: how to get artwork from an mp3 file? ), But the code is written in Objective C. I just want to capture the image embedded in my MP3 and display it in my ImageView.

I looked at the API documentation for MPMediaItemArtwork and found an example that also does what I try to execute in Objective-C also here ( http://www.codeitive.com/0zHjkUjUWX/not-able-to-get-the-uiimage-from -mpmediaitempropertyartwork.html ) but cannot find 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 screenshot of my .mp3 file. As you can see, there really are album covers that are also visible in the β€œget info” section of the Finder. I also opened .mp3 in my iTunes to make sure, and confirmed that there are illustrations in the Get Info section there, as well as on the Jobs tab.

However, trying to use commonKey to assign an image to my ImageView, I found that there is no ordinary key for "artwork".

sample MP3 data

thanks

+5
source share
3 answers

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

I added println lines commented out in the places you are interested in. Feel free to sort it out to see what happens.

  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: Clear this code a bit

 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 } } 
+6
source

Try it like this:

 let audioInfo = MPNowPlayingInfoCenter.defaultCenter() var nowPlayingInfo:[String:AnyObject] = [:] let playerItem = AVPlayerItem(URL: url) let metadataList = playerItem.asset.metadata for item in metadataList { if item.commonKey != nil && item.value != nil { if item.commonKey == "title" { println(item.stringValue) nowPlayingInfo[MPMediaItemPropertyTitle] = item.stringValue } if item.commonKey == "type" { println(item.stringValue) nowPlayingInfo[MPMediaItemPropertyGenre] = item.stringValue } if item.commonKey == "albumName" { println(item.stringValue) nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = item.stringValue } if item.commonKey == "artist" { println(item.stringValue) nowPlayingInfo[MPMediaItemPropertyArtist] = item.stringValue } if item.commonKey == "artwork" { if let image = UIImage(data: item.dataValue) { nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(image: image) println(image.description) } } } } audioInfo.nowPlayingInfo = nowPlayingInfo 

Note. You will need to call beginReceivingRemoteControlEvents (), otherwise it will not work on the device itself. You will also need to set the background resolution of the application (Audio and AirPlay) and set your AVAudioSession category to AVAudioSessionCategoryPlayback and set it active:

 do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) print("AVAudioSession Category Playback OK") do { try AVAudioSession.sharedInstance().setActive(true) print("AVAudioSession is Active") } catch let error as NSError { print(error.localizedDescription) } } catch let error as NSError { print(error.localizedDescription) } 

enter image description here

+3
source

Try the following:

It seems that sometimes iOS 8 returns nil on the first attempt to get this information:

 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
source

All Articles