我找到了这个解决方案(iOS AVFoundation: How do I fetch artwork from an mp3 file?),但代码是用Objective C编写的.我只是想抓住嵌入MP3中的图像并将其显示在我的ImageVIEw中.
我已经查看了MPMediaItemArtwork的api文档,并找到了一个示例,它也完成了我在Objective C中尝试完成的任务(http://www.codeitive.com/0zHjkUjUWX/not-able-to-get-the-uiimage-from-mpmediaitempropertyartwork.html),但无法提出解决方案.我的代码如下:
@H_419_5@import UIKitimport AVFoundationimport MediaPlayerclass 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.}}这是我的示例.mp3文件的屏幕截图.正如您所看到的,确实有专辑图片在Finder的“获取信息”部分中都可见.我还在我的iTunes中打开了.mp3,以确保并确认其中的“获取信息”部分以及“艺术品”标签下都有艺术品.
但是,当尝试使用commonKey将图像分配给我的imageVIEw时,我发现“艺术品”没有commonKey.
谢谢
将您的代码片段更改为此(我已经测试过):@H_419_5@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 } } } }I added println lines commented in places of interest,Feel free to uncomment in order to see what is happening.
@H_419_5@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: A bit of clean up of this code
@H_419_5@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 }} 总结UPDATE: For Swift 4
以上是内存溢出为你收集整理的swift – 显示.MP3文件的图稿全部内容,希望文章能够帮你解决swift – 显示.MP3文件的图稿所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)