从音乐库ios swift 3 xcode 8.2.1获取所有歌曲

时间:2017-04-13 06:43:54

标签: ios swift3

我想从音乐库中获取所有歌曲并在我的表格视图中显示,然后在表格视图中选择多首歌曲并在AVAudioPlayer中播放。但是我无法从音乐库中获取歌曲并在我的桌面视图中显示。我访问了很多链接但是对我不起作用,请帮助我。这是我的代码

@IBAction func selectMusicFromGallery(_ sender: Any) 
{
    mediaPicker = MPMediaPickerController(mediaTypes: .music)
    mediaPicker.allowsPickingMultipleItems = false
    mediaPicker.delegate = self
   // mediaPicker.prompt = "Select Songs"
    self.present(mediaPicker, animated: true, completion: nil)
}

func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) 
{
    let mediaItems = MPMediaQuery.songs().items
    let mediaCollection = MPMediaItemCollection(items: mediaItems!)
    let player = MPMusicPlayerController.systemMusicPlayer()
    player.setQueue(with: mediaCollection)
    player.play()
}

1 个答案:

答案 0 :(得分:10)

您可以使用此SongQuery.swift类

//     // SongQuery.swift     //

public List<UserInfo> getProfiles(int userHandle) {
    try {
        return mService.getProfiles(userHandle, false /* enabledOnly */);
    } catch (RemoteException re) {
        Log.w(TAG, "Could not get user list", re);
        return null;
    }
}

对于View Controller Code在表视图控制器中显示歌曲

import Foundation
import MediaPlayer

struct SongInfo {

    var albumTitle: String
    var artistName: String
    var songTitle:  String

    var songId   :  NSNumber
}

struct AlbumInfo {

    var albumTitle: String
    var songs: [SongInfo]
}

class SongQuery {

    func get(songCategory: String) -> [AlbumInfo] {

        var albums: [AlbumInfo] = []
         let albumsQuery: MPMediaQuery
        if songCategory == "Artist" {
            albumsQuery = MPMediaQuery.artists()

        } else if songCategory == "Album" {
            albumsQuery = MPMediaQuery.albums()

        } else {
            albumsQuery = MPMediaQuery.albums()
        }


       // let albumsQuery: MPMediaQuery = MPMediaQuery.albums()
        let albumItems: [MPMediaItemCollection] = albumsQuery.collections! as [MPMediaItemCollection]
      //  var album: MPMediaItemCollection

        for album in albumItems {

            let albumItems: [MPMediaItem] = album.items as [MPMediaItem]
           // var song: MPMediaItem

            var songs: [SongInfo] = []

            var albumTitle: String = ""

            for song in albumItems {
                if songCategory == "Artist" {
                    albumTitle = song.value( forProperty: MPMediaItemPropertyArtist ) as! String

                } else if songCategory == "Album" {
                    albumTitle = song.value( forProperty: MPMediaItemPropertyAlbumTitle ) as! String


                } else {
                    albumTitle = song.value( forProperty: MPMediaItemPropertyAlbumTitle ) as! String
                }

                let songInfo: SongInfo = SongInfo(
                    albumTitle: song.value( forProperty: MPMediaItemPropertyAlbumTitle ) as! String,
                    artistName: song.value( forProperty: MPMediaItemPropertyArtist ) as! String,
                    songTitle:  song.value( forProperty: MPMediaItemPropertyTitle ) as! String,
                    songId:     song.value( forProperty: MPMediaItemPropertyPersistentID ) as! NSNumber
                )
                songs.append( songInfo )
            }

            let albumInfo: AlbumInfo = AlbumInfo(

                albumTitle: albumTitle,
                songs: songs
            )

            albums.append( albumInfo )
        }

        return albums

    }

    func getItem( songId: NSNumber ) -> MPMediaItem {

        let property: MPMediaPropertyPredicate = MPMediaPropertyPredicate( value: songId, forProperty: MPMediaItemPropertyPersistentID )

        let query: MPMediaQuery = MPMediaQuery()
        query.addFilterPredicate( property )

        var items: [MPMediaItem] = query.items! as [MPMediaItem]

        return items[items.count - 1]

    }

}

**

  

不要忘了添加框架MediaPlayer.framework,   MediaToolbox.framework,AudioToolbox.framework

**