0

我正在使用此代码获取艺术品,但这对我来说不是锻炼。这段代码有什么问题。建议我。谢谢。

MPMediaQuery *mySongsQuery = [MPMediaQuery songsQuery];
            NSArray *SongsList = [mySongsQuery collections];
            for (MPMediaItemCollection *SongsArt in SongsList) {
NSArray *songs = [SongsArt items];
                for (MPMediaItem *song in songs) {

                    if ([(MPMediaItem*)item valueForProperty:MPMediaItemPropertyAssetURL] != nil) {
                    CGSize artworkImageViewSize = CGSizeMake(40, 40);
                    MPMediaItemArtwork *artwork = [song valueForProperty:MPMediaItemPropertyArtwork];
                    UIImage * image = [artwork imageWithSize:artworkImageViewSize];
                    if (image!= nil)
                    {
                        imgv_songImageView.image = image;
                    }
                    else
                    {
                        imgv_songImageView.image = [UIImage imageNamed:@"musicD-jpeg.png"];
                    }
       }
    }
4

2 回答 2

0

我假设您只想循环播放音乐库中的所有歌曲,因此我认为不需要收藏:

MPMediaQuery *mySongsQuery = [MPMediaQuery songsQuery];

for (MPMediaItem *item in mySongsQuery.items) {
  if (![[item valueForProperty:MPMediaItemPropertyIsCloudItem]boolValue]) {

    CGSize artworkImageViewSize = CGSizeMake(40, 40);
    MPMediaItemArtwork *artwork = [song valueForProperty:MPMediaItemPropertyArtwork];
    UIImage *image = [artwork imageWithSize:artworkImageViewSize];

    if (image) {
      imgv_songImageView.image = image;
    } else {
      imgv_songImageView.image = [UIImage imageNamed:@"musicD-jpeg.png"];
    }
  }
}

我不确定您为什么要检查资产 URL,但我已将其保留。

于 2014-09-08T10:45:46.293 回答
0

在这里,我发布了获取曲目并按字母顺序排序的代码。它是用swift3编写的。

    /// Get all the songs in the device and display in the tableview
///
func getAllSongs() {
    let query: MPMediaQuery = MPMediaQuery.songs()
    let allSongs = query.items

    allSongItems?.removeAll()

    guard allSongs != nil else {
        return
    }

    var index = 0
    for item in allSongs! {
        let pathURL: URL? = item.value(forProperty: MPMediaItemPropertyAssetURL) as? URL
        if pathURL == nil {
            print("@Warning!!! Track : \(item) is not playable.")
        } else {
            let trackInfo = SongItem()
            trackInfo.index = index
            trackInfo.mediaItem = item

            let title = item.value(forProperty: MPMediaItemPropertyTitle) as? String ?? "<Unknown>"
            let artistName = item.value(forProperty: MPMediaItemPropertyArtist) as? String ?? "<Unknown>"
            trackInfo.songName = title
            trackInfo.artistName = artistName

            trackInfo.isSelected = false
            trackInfo.songURL = item.value(forProperty: MPMediaItemPropertyAssetURL) as? URL
            allSongItems?.append(trackInfo)
            index += 1
        }
    }

    // Sort the songs alphabetically
    let sortedArray: [SongItem]? = allSongItems?.sorted {
        $0.songName!.localizedCompare($1.songName!) == .orderedAscending
    }
    allSongItems?.removeAll()
    if let arr = sortedArray {
        allSongItems?.append(contentsOf: arr)
    }


}
于 2016-10-03T05:23:37.123 回答