2015-07-03 46 views
0

我正在做一個測試程序處理音頻和tableView,現在我有控制器工作並正確播放所選歌曲,但我試圖顯示從MP3文件的元數據,並沒有做任何事情目前我有這樣的:Swift和XCode 7 beta 2沒有顯示正在播放的歌曲的元數據

功能打印數據:

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測試版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 
+0

添加更多日誌記錄。你需要一個'for..in'循環和許多'如果讓' - 你需要知道你實際上在每一箇中獲得了什麼。 – matt

+0

順便說一下,這是愚蠢的:'元數據作爲[AVMetadataItem]' – matt

+0

是的,我現在有它像var metaStuff:[AVMetadataItem] = secondTestTitle.asset.commonMetadata as! [AVMetadataItem],我得到空[]和0計數 –

回答

1

如果這是你的應用程序包的MP3文件,然後我們假設它被稱爲test.mp3。然後你可以這樣說:

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

如果你在控制檯中看不到任何元數據,那麼這個文件沒有元數據。

但是,這不是我會做的,因爲它不是一個非常現實的測試 - 並且它不太可能非常有用,因爲大多數信息不會作爲元數據寫入文件。更可能的是,您將想要了解用戶音樂庫中的歌曲。在這種情況下,您可以使用媒體播放器框架,如我所描述的​​。

+0

是的,我看到元,正在工作謝謝,我正在使用NSURL(字符串:fileToPlay)!不urlforresource我不知道有什麼區別。 –

相關問題