2017-08-06 132 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 –

相关问题