swift – 显示.MP3文件的图稿

前端之家收集整理的这篇文章主要介绍了swift – 显示.MP3文件的图稿前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在 ImageView中显示本地存储的.MP3轨道的专辑图片.有谁知道如何在 Swift获取这件艺术品才能实现这一目标?

我找到了这个解决方案(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),但无法提出解决方案.我的代码如下:

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.
}


}

这是我的示例.mp3文件的屏幕截图.正如您所看到的,确实有专辑图片在Finder的“获取信息”部分中都可见.我还在我的iTunes中打开了.mp3,以确保并确认其中的“获取信息”部分以及“艺术品”标签下都有艺术品.

但是,当尝试使用commonKey将图像分配给我的imageView时,我发现“艺术品”没有commonKey.

谢谢

将您的代码片段更改为此(我已经测试过):

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
   }
}
原文链接:https://www.f2er.com/swift/318721.html

猜你在找的Swift相关文章