2017-08-06 149 views
1

我想構建一個應用程序,可以獲取設備上正在播放的歌曲的名稱(例如,如果我使用Spotify或任何其他播放器播放一首歌曲,應用程序應該能夠獲得標題)。標題應該只存儲在一個變量中。Swift IOS:獲取當前播放歌曲的標題

我還沒有成功地使用nowPlayingInfo或MPMediaItemPropertyTitle。

有人可以幫助我嗎?這是我嘗試過的代碼,但是我得到一個錯誤(使用未解析的標識符'playerItem'):let metadataList = playerItem.asset.metadata as! [AVMetadataItem]

import UIKit 
import MediaPlayer 
import AVFoundation 
class ViewController: UIViewController { 


var url:NSURL! 
let audioInfo = MPNowPlayingInfoCenter.default() 
var nowPlayingInfo:[String:Any] = [:] 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    print(url.path!) 

    let metadataList = playerItem.asset.metadata as! [AVMetadataItem] 
    for item in metadataList { 
     if item.commonKey != nil && item.value != nil { 
      if item.commonKey == "title" { 
       println(item.stringValue) 
       nowPlayingInfo[MPMediaItemPropertyTitle] = item.stringValue 
      } 
      if item.commonKey == "type" { 
       println(item.stringValue) 
       nowPlayingInfo[MPMediaItemPropertyGenre] = item.stringValue 
      } 
      if item.commonKey == "albumName" { 
       println(item.stringValue) 
       nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = item.stringValue 
      } 
      if item.commonKey == "artist" { 
       println(item.stringValue) 
       nowPlayingInfo[MPMediaItemPropertyArtist] = item.stringValue 
      } 
      if item.commonKey == "artwork" { 
       if let image = UIImage(data: item.value as! NSData) { 
        nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(image: image) 
        println(image.description) 
       } 
      } 
     } 
    } 
    audioInfo.nowPlayingInfo = nowPlayingInfo 
+0

你能分享一些代碼,你試過嗎? – Sal

+0

Paul我添加了一些代碼 – Kenneth

回答

-2

我想你好想代碼:var playerItem = AVPlayerItem(URL: url)

let metadataList = playerItem.asset.metadata as! [AVMetadataItem] 

前如果你在本地文件夾中的幾個的音樂,並希望得到的藝術品,標題,藝術家和專輯在音樂中,您可以創建一個名爲Music的模型,以及一種獲取包含所需信息的musicArray的方法。 就是這麼簡單,你可以使用var musics :[Music]?保存在的viewController的音樂,並與方法獲取其排序由音樂標題(或其他財產要使用)的音樂:

private func getMusics(){ 
     Music.getMusicList { [unowned self] (musics) in 
      self.musics = musics.sorted{$0.title! < $1.title!} 
     } 
    } 

在該模型中的音樂文件:

import Foundation 
import UIKit 
import AVFoundation 

struct Music { 
    var artwork : UIImage? 
    var title  : String? 
    var artist : String? 
    var album  : String? 
    var muscicURL : String? 

     init(artwork:UIImage,title:String,artist:String,album:String,muscicURL:String) { 
     self.artwork = artwork 
     self.title = title 
     self.artist = artist 
     self.album = album 
     self.muscicURL = muscicURL 

    } 
static func getMusicList(completion:@escaping ([Music]) -> Void){ 

     var musicImage : UIImage? 
     var musicTitle : String? 
     var musicArtist : String? 
     var musicAlbum : String? 
     var musicURL : String! 

    var musics = [Music]() 

    let folderURL = URL(fileURLWithPath: Bundle.main.resourcePath!) 

    do { 
     let songPaths = try FileManager.default.contentsOfDirectory(at: folderURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles) 

     _ = songPaths.map({ 
      let songPath = $0.absoluteString 
      if songPath.contains(".mp3"){ 

       musicURL = songPath 
       let mp3Asset = AVURLAsset.init(url: URL(string:songPath)!) 

       for metadataFormat in mp3Asset.availableMetadataFormats { 
        for metadataItem in mp3Asset.metadata(forFormat: metadataFormat){ 

         if metadataItem.commonKey == "artwork",let imageData = metadataItem.value as? Data{ 

          musicImage = UIImage.init(data: imageData)! 

         }else if metadataItem.commonKey == "title",let title = metadataItem.value as? String { 

          musicTitle = title 

         }else if metadataItem.commonKey == "artist",let artist = metadataItem.value as? String { 

          musicArtist = artist 

         }else if metadataItem.commonKey == "albumName",let album = metadataItem.value as? String { 

          musicAlbum = album 
         } 

        } 

       } 
       let music = Music(artwork: musicImage!, title: musicTitle!, artist: musicArtist!, album: musicAlbum!,muscicURL:musicURL!) 
       musics.append(music) 

      } 
     }) 
     completion(musics) 
     // print(musics) 

    } catch let error { 
     print(error.localizedDescription) 

    } 
} 

} 
+0

這裏是一個有用的參考:https://stackoverflow.com/questions/28979013/how-do-i-get-the-album-artwork-of-the-currently-playing-music-using -swift/48035857#48035857 –

相關問題