Sto tentando di visualizzare attualmente la grafica dell'album per una traccia .MP3 memorizzata localmente in un ImageView. Qualcuno sa come recuperare questa grafica in Swift per realizzare questo?Visualizzazione grafica per file .MP3
Ho trovato questa soluzione (iOS AVFoundation: How do I fetch artwork from an mp3 file?) ma il codice è scritto nell'obiettivo C. Voglio semplicemente catturare l'immagine incorporata nel mio MP3 e visualizzarla nel mio ImageView.
Ho esaminato la documentazione API per MPMediaItemArtwork e ho trovato un esempio che realizza anche quello che sto cercando di realizzare anche in Objective C (http://www.codeitive.com/0zHjkUjUWX/not-able-to-get-the-uiimage-from-mpmediaitempropertyartwork.html) ma non riesco a trovare una soluzione. Il mio codice è il seguente:
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.
}
}
Ecco una schermata del mio esempio di file .mp3. Come puoi vedere, in effetti è visibile la copertina dell'album nella sezione "Ottieni informazioni" di Finder. Ho anche aperto la .mp3 nel mio iTunes per assicurarmi e ho confermato che ci sono delle illustrazioni nella sezione "Ottieni informazioni" e nella scheda "Grafica".
Tuttavia, quando si tenta di utilizzare commonKey per assegnare l'immagine al mio imageView, trovo che non esiste commonkey per "artwork".
Grazie
Perfetto! Questa è stata la soluzione che ha funzionato per me! Grazie per l'aiuto. Quindi era corretto usare .metadata invece di .commonmetadata? È una copertura solo più approfondita dei metadati rispetto agli altri? Complimenti e saluti! – Unconquered82