【问题标题】:Swift and XCode 7 beta 2 are not displaying the metadata of the songs playingSwift 和 XCode 7 beta 2 不显示正在播放的歌曲的元数据
【发布时间】: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 计数
  • 前几行返回 [] 所以甚至没有开始。
  • 它正在“开始”。它是空的。

标签: ios xcode swift


【解决方案1】:

如果这是您的应用程序包中的 MP3 文件,那么假设它被称为 test.mp3。然后你可以说:

let url = NSBundle.mainBundle().URLForResource("test", withExtension:"mp3")
let asset = AVAsset(URL: url!)
let meta = asset.metadata
print(meta)

如果您在说这句话时在控制台中没有看到任何元数据,则该文件没有元数据。

但是,我不会这样做,因为这不是一个非常现实的测试——而且它不太可能很有用,因为大多数信息都不会作为元数据写入文件。更有可能的是,您想了解用户音乐库中的一首歌曲。在这种情况下,您将使用媒体播放器框架,正如我所描述的 in my book

【讨论】:

  • 是的,我看到了元数据,正在工作,谢谢,我使用的是 NSURL(string: fileToPlay)!不是 urlforresource 我不知道有什么区别。
猜你喜欢
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多