【发布时间】:2015-07-03 15:42:34
【问题描述】:
我正在做一个处理音频和 tableView 的测试程序,现在我的控制器正在工作并正确播放所选歌曲,但我正在尝试显示 mp3 文件中的元数据并且目前我没有做任何事情像这样:
打印数据的功能:
func testAudioStuff(testURL testURL: NSURL) {
let audioInfo = AVPlayerItem(URL: testURL)
let metaDataList = audioInfo.asset.metadata as [AVMetadataItem]
//here it should print something it does not
for item in metaDataList {
if item.commonKey == nil {
continue
}
if let key = item.commonKey, let value = item.value {
print(key)
print(value as! String)
if key == "title" {
print(key)
print("here is the title" + (value as! String))
}
if key == "artist" {
print(key)
print("here is the artist" + (value as! String))
}
if key == "artwork" {
print("here is the artwork" + (value as! String))
}
}
}
}
播放音乐的功能:
func playSelectedMusic(song: Int, section: Int) {
if section == 1 {
//print(NSBundle.mainBundle().description + "\(song) \(section)")
if let starMusic = productsAndMusic["Music"] {
//print("We are looking at \(starMusic[song])")
let play = starMusic[song].componentsSeparatedByString(".")[0]
//print(play)
if let fileToPlay = NSBundle.mainBundle().pathForResource(play, ofType: "mp3") {
//print(fileToPlay)
testAudioStuff(testURL: NSURL(string: fileToPlay)!)
do {
player = try AVAudioPlayer(contentsOfURL: NSURL(string: fileToPlay)!)
} catch Errors.GeneralError {
} catch let error as NSError {
print("i dunno this does not comes with instructions \(error)")
} catch let something as ErrorType {
print("somehting else \(something)")
}
player.prepareToPlay()
player.play()
}
//player = AVAudioPlayer(contentsOfURL: NSURL(string: NSBundle.mainBundle().pathForResource(play, ofType: "mp3")!))
//player.prepareToPlay()
//print(NSBundle.mainBundle().pathForResource(play, ofType: "mp3"))
}
//print(NSBundle.mainBundle().pathForResource((productsAndMusic["Music"]![song] as String).componentsSeparatedByString(".")[0], ofType: "mp3")!)
//print((productsAndMusic["Music"]![song] as String).componentsSeparatedByString(".")[0])
} else {
}
}
正如我提到的,它在模拟器中播放选定的歌曲,但不打印元数据,为什么?有什么帮助吗?在 Xcode 7 beta 2 中。
我尝试了一些东西,但没有骰子,这个:
var secondTestTitle: AVAsset = AVAsset(URL: testURL)
print(secondTestTitle.description)
var metaStuff: [AVMetadataItem] = secondTestTitle.commonMetadata as [AVMetadataItem]
print(metaStuff.count)
结果:
<AVURLAsset: 0x78ea3930, URL = /Users/pedro/Library/Developer/CoreSimulator/Devices/304FE5A7-9506-4A9B-B685-5CDBE9AFB4C4/data/Containers/Bundle/Applica ... ock1.mp3>
0
【问题讨论】:
-
添加更多日志记录。你有一个
for..in循环和很多if let- 你需要知道你在每个循环中实际得到了什么。 -
顺便说一句,这很愚蠢:
metadata as [AVMetadataItem] -
是的,我现在拥有它,就像 var metaStuff: [AVMetadataItem] = secondTestTitle.asset.commonMetadata 一样! [AVMetadataItem],我变空了 [] 和 0 计数
-
前几行返回 [] 所以甚至没有开始。
-
它正在“开始”。它是空的。