swift - Displaying Artwork for .MP3 file -
i trying display album artwork locally stored .mp3 track in imageview. know how fetch artwork in swift in order accomplish this?
i have found solution (ios avfoundation: how fetch artwork mp3 file?) code written in objective c. want grab image embedded in mp3 , display in imageview.
i've looked @ api documentation mpmediaitemartwork , found example accomplishes trying accomplish in objective c here(http://www.codeitive.com/0zhjkujuwx/not-able-to-get-the-uiimage-from-mpmediaitempropertyartwork.html) cannot come solution. code 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] 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 resources can recreated. } }
here screen shot of sample .mp3 file. can see there indeed album artwork both visible in "get info" section of finder. i've opened .mp3 in itunes make sure , have confirmed there artwork in "get info" section of there under "artwork" tab.
however, when trying use commonkey assign image imageview find there no commonkey "artwork".
thanks
change snippet of code (i tested it):
i added println lines commented in places of interest, feel free uncomment in order see happening.
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: bit of clean of 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" value nsdata : artistimage.image = uiimage(data: value as! nsdata) default: continue } }
Comments
Post a Comment