ios - Swift 和 XCode 7 beta 2 不显示正在播放的歌曲的元数据

标签 ios xcode swift

我正在做一个处理音频和 tableView 的测试程序,现在我有 Controller 工作并正确播放所选歌曲,但我正在尝试显示 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

最佳答案

如果这是您的应用程序包中的 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 .

关于ios - Swift 和 XCode 7 beta 2 不显示正在播放的歌曲的元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31210397/

相关文章:

ios - 证书、标识符和配置文件页面无法在 developer.apple 中访问

ios - CollectionView 委托(delegate)在 URL 连接完成之前调用

ios - 注销并清除当前 VC 以下的 VC

ios - 无效的 App Store 图标

swift - Xcode 13 Tuist 生成失败

swift - 如何在不使 CPU 使用率最大化的情况下显示大量 GMSPolylines?

c# - 如何在 Monotouch 中子类化 UIApplication?

iphone - IB - 无法为 View Controller 分配类

ios - 如何正确地将弱引用传递给函数的调用者?

ios - navigationAction.navigationType == .linkActivated 什么时候执行?