从音乐曲目获取信息的问题

时间:2017-05-30 15:16:03

标签: ios iphone swift avplayeritem

我想从音乐曲目中获取一些信息,一切正常,但有些音乐没有titleartistcover artwork,在这种情况下,我必须提供一个例如Unknown的默认值。

我正在使用这样的跟踪信息填充表格视图:

//MARK: - Get Data From Track :
func getDataFromTrack(file:URL)  {

    //Get data from track
    urlAsset = AVURLAsset(url: file)
    for format in urlAsset.availableMetadataFormats {

        for metadata in urlAsset.metadata(forFormat: format) {

            //Track Name :
            if metadata.commonKey == "title" {
                if let title = metadata.stringValue {
                    appDefaults.setTrack(Name: title)
                } else {
                    appDefaults.setTrack(Name: "Unknown")
                }
            }



                //Track Artist:
            if metadata.commonKey == "artist" {
                    if let artist = metadata.stringValue {
                        appDefaults.setArtist(Name: artist)
                    } else {
                        appDefaults.setArtist(Name: "Unknown")
                    }
                }


            //Track Cover:
            if metadata.commonKey == "artwork" {
                if let data = metadata.dataValue {
                    let image = UIImage(data: data)
                    appDefaults.setCover(Image: image!)
                }  else {
                    appDefaults.setCover(Image: #imageLiteral(resourceName: "Defualt cover"))
                }
            }


            appDefaults.setTrack(duration: Float(CMTimeGetSeconds(urlAsset.duration)))
        }
    }
}

在表格视图中:

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


... setup cell 

 let fileName = player.MP3DirectoryFiles[indexPath.row-2] as! NSString
            let absoluteFileName = String(describing: fileName.lastPathComponent)
            let removeSpaceString = absoluteFileName.replacingOccurrences(of: "%20", with: " ")


            //Get MP3 Infromations
            let documentsURL = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!,isDirectory: true )
            let urlToMyPath = documentsURL.appendingPathComponent(removeSpaceString)!

            player.getDataFromTrack(file: urlToMyPath)

}

如果没有来自轨道单元的数据填充其数据,例如带有其他轨道信息的标题!

我试图获得这样的零值,但没有帮助我:

if metadata.commonKey == "artist" {
                    if let artist = metadata.stringValue {
                        appDefaults.setArtist(Name: artist)
                    } else {
                        appDefaults.setArtist(Name: "Unknown")
                    }
                }

1 个答案:

答案 0 :(得分:1)

首先尝试设置默认值,然后在找到资产的相应标记时更新它们:

//MARK: - Get Data From Track :
func getDataFromTrack(file:URL)  {
    // Set defaults
    appDefaults.setTrack(Name: "Unknown")
    appDefaults.setArtist(Name: "Unknown")
    appDefaults.setCover(Image: #imageLiteral(resourceName: "Defualt cover"))
    appDefaults.setTrack(duration: Float(CMTimeGetSeconds(urlAsset.duration)))

    //Get data from track
    urlAsset = AVURLAsset(url: file)
    for format in urlAsset.availableMetadataFormats {
        for metadata in urlAsset.metadata(forFormat: format) {

            //Track Name :
            if metadata.commonKey == "title" {
                if let title = metadata.stringValue, !title.isEmpty {
                    appDefaults.setTrack(Name: title)
                }
            }

            //Track Artist:
            if metadata.commonKey == "artist" {
                if let artist = metadata.stringValue, !artist.isEmpty {
                    appDefaults.setArtist(Name: artist)
                }
            }

            //Track Cover:
            if metadata.commonKey == "artwork" {
                if let data = metadata.dataValue {
                    let image = UIImage(data: data)
                    appDefaults.setCover(Image: image!)
                }
            }
        }
    }
}

如果资产不包含相应的密钥,则永远不会调用您尝试设置默认值的代码。此外,在for循环中设置轨道持续时间将导致多次不必要地执行它。