Last active
July 30, 2019 20:42
-
-
Save calmery/cf9f3c151e0a519a80445c7927686fbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import MediaPlayer | |
@objc(MusicLibrary) class MusicLibrary : CDVPlugin { | |
func getAlbums(_ command: CDVInvokedUrlCommand) { | |
let albumItems = MPMediaQuery.albums().items as [MPMediaItem]? | |
if albumItems == nil { | |
let result = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: []) | |
self.commandDelegate!.send(result, callbackId: command.callbackId) | |
return | |
} | |
var albums: [Dictionary<String, Any>] = [] | |
for songItem in albumItems! { | |
var data = Dictionary<String, Any>() | |
// Media Item Properties | |
// https://developer.apple.com/documentation/mediaplayer/mpmediaitem | |
// var albumArtist: String? | |
// The primary performing artist for an album as a whole. | |
data["albumArtist"] = songItem.albumArtist ?? "" | |
// var albumArtistPersistentID: MPMediaEntityPersistentID | |
// The persistent identifier for the primary performing artist for an album as a whole. | |
data["albumArtistPersistentID"] = songItem.albumArtistPersistentID | |
// var albumPersistentID: MPMediaEntityPersistentID | |
// The persistent identifier for an album. | |
data["albumPersistentID"] = songItem.albumPersistentID | |
// var albumTitle: String? | |
// The title of an album, such as “Live On Mars”, as opposed to the title of an individual song on the album, such as “Crater Dance (radio edit)”. | |
data["title"] = songItem.title ?? "" | |
// var albumTrackCount: Int | |
// The number of tracks in the album that contains the media item. | |
data["albumTrackCount"] = songItem.albumTrackCount | |
// var albumTrackNumber: Int | |
// The track number of the media item, for a media item that is part of an album. | |
data["albumTrackNumber"] = songItem.albumTrackNumber | |
// var artist: String? | |
// The performing artist(s) for a media item—which may vary from the primary artist for the album that a media item belongs to. | |
data["artist"] = songItem.artist ?? "" | |
// var artistPersistentID: MPMediaEntityPersistentID | |
// The persistent identifier for an artist. | |
data["artistPersistentID"] = songItem.artistPersistentID | |
// var artwork: MPMediaItemArtwork? | |
// The artwork image for the media item. | |
data["artwork"] = { | |
if songItem.artwork == nil { return "" } | |
let image = songItem.artwork!.image(at: /* CGSize(width: 100, height: 100) */ songItem.artwork!.bounds.size) | |
if image == nil { return "" } | |
let png = UIImagePNGRepresentation(image!) | |
if png == nil { return "" } | |
return png!.base64EncodedString() | |
}() | |
// var assetURL: URL? | |
// The URL pointing to the media item. | |
data["assetURL"] = songItem.assetURL != nil ? songItem.assetURL!.absoluteString : "" | |
// var beatsPerMinute: Int | |
// The number of musical beats per minute for the media item. | |
data["beatsPerMinute"] = songItem.beatsPerMinute | |
// var bookmarkTime: TimeInterval (Alias Double) | |
// The user’s place in the media item the most recent time it was played. | |
data["bookmarkTime"] = songItem.bookmarkTime | |
// var isCloudItem: Bool | |
// A Boolean value indicating whether the media item is an iCloud Music Library item. | |
data["isCloudItem"] = songItem.isCloudItem | |
// var comments: String? | |
// Textual information about the media item. | |
data["comments"] = songItem.comments ?? "" | |
// var isCompilation: Bool | |
// A Boolean value indicating whether the media item is part of a compilation. | |
data["isCompilation"] = songItem.isCompilation | |
// var composer: String? | |
// The musical composer for the media item. | |
data["composer"] = songItem.composer ?? "" | |
// var composerPersistentID: MPMediaEntityPersistentID | |
// The persistent identifier for a composer. | |
data["composerPersistentID"] = songItem.composerPersistentID | |
// var dateAdded: Date | |
// The date the item was added to the library. | |
data["dateAdded"] = songItem.dateAdded.timeIntervalSince1970 | |
// var discCount: Int | |
// The number of discs in the album that contains the media item. | |
data["discCount"] = songItem.discCount | |
// var discNumber: Int | |
// The disc number of the media item, for a media item that is part of a multi-disc album. | |
data["discNumber"] = songItem.discNumber | |
// var isExplicitItem: Bool | |
// A Boolean value that indicates whether the item has explicit (adult) lyrics or language. | |
data["isExplicitItem"] = songItem.isExplicitItem | |
// var genre: String? | |
// The musical or film genre of the media item. | |
data["genre"] = songItem.genre ?? "" | |
// var genrePersistentID: MPMediaEntityPersistentID | |
// The persistent identifier for a genre. | |
data["genrePersistentID"] = songItem.genrePersistentID | |
// var lastPlayedDate: Date? | |
// The date a media item was last played. | |
data["lastPlayedDate"] = songItem.lastPlayedDate != nil ? songItem.lastPlayedDate!.timeIntervalSince1970 : 0 | |
// var lyrics: String? | |
// The lyrics for the media item. | |
data["lyrics"] = songItem.lyrics ?? "" as String | |
// var mediaType: MPMediaType | |
// The media type of the media item. | |
data["mediaType"] = { | |
switch songItem.mediaType { | |
// Audio | |
case MPMediaType.music: | |
return "music" | |
case MPMediaType.podcast: | |
return "podcast" | |
case MPMediaType.audioBook: | |
return "audioBook" | |
case MPMediaType.audioITunesU: | |
return "audioITunesU" | |
case MPMediaType.anyAudio: | |
return "anyAudio" | |
// Video | |
case MPMediaType.movie: | |
return "movie" | |
case MPMediaType.tvShow: | |
return "tvShow" | |
case MPMediaType.videoPodcast: | |
return "videoPodcast" | |
case MPMediaType.musicVideo: | |
return "musicVideo" | |
case MPMediaType.videoITunesU: | |
return "videoITunesU" | |
case MPMediaType.homeVideo: | |
return "homeVideo" | |
case MPMediaType.anyVideo: | |
return "anyVideo" | |
// Any | |
default: | |
return "any" | |
} | |
}() | |
// var persistentID: MPMediaEntityPersistentID (Alias UInt64) | |
// The persistent identifier for the media item. | |
data["persistentID"] = songItem.persistentID | |
// var playCount: Int | |
// The number of times the user has played the media item. | |
data["playCount"] = songItem.playCount | |
// var playbackDuration: TimeInterval | |
// The playback duration of the media item. | |
data["playbackDuration"] = songItem.playbackDuration | |
// var playbackStoreID: String | |
// The non-library identifier for a media item. | |
data["playbackStoreID"] = songItem.playbackStoreID | |
// var podcastPersistentID: MPMediaEntityPersistentID | |
// The persistent identifier for an audio podcast. | |
data["podcastPersistentID"] = songItem.podcastPersistentID | |
// var podcastTitle: String? | |
// The title of a podcast, such as “This Martian Drudgery”, as opposed to the title of an individual episode of a podcast such as “Episode 12: Another Cold Day At The Pole”. | |
data["podcastTitle"] = songItem.podcastTitle ?? "" | |
// var hasProtectedAsset: Bool | |
// A Boolean indicating whether the media item has a protected asset. | |
data["hasProtectedAsset"] = songItem.hasProtectedAsset | |
// var rating: Int | |
// The user-specified rating of the object in the range [0...5], where a value of 5 indicates the most favorable rating. | |
data["rating"] = songItem.rating | |
// var releaseDate: Date? | |
// The date on which the media item was first publicly released. | |
data["releaseDate"] = songItem.releaseDate != nil ? songItem.releaseDate!.timeIntervalSince1970 : 0 | |
// var skipCount: Int | |
// The number of times the user has skipped playing the item. | |
data["skipCount"] = songItem.skipCount | |
// var title: String? | |
// The title (or name) of the media item. | |
data["title"] = songItem.title ?? "" | |
// var userGrouping: String? | |
// Grouping information for the media item. | |
data["userGrouping"] = songItem.userGrouping ?? "" | |
// Output | |
songs.append( data ) | |
} | |
let result = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: albumItems) | |
self.commandDelegate!.send(result, callbackId: command.callbackId) | |
} | |
func getAll(_ command: CDVInvokedUrlCommand) { | |
let songItems: [MPMediaItem] = MPMediaQuery.songs().items! as [MPMediaItem] | |
var songs: [Dictionary<String, Any>] = [] | |
for songItem in songItems[0...5] { | |
var data = Dictionary<String, Any>() | |
// Media Item Properties | |
// https://developer.apple.com/documentation/mediaplayer/mpmediaitem | |
// var albumArtist: String? | |
// The primary performing artist for an album as a whole. | |
data["albumArtist"] = songItem.albumArtist ?? "" | |
// var albumArtistPersistentID: MPMediaEntityPersistentID | |
// The persistent identifier for the primary performing artist for an album as a whole. | |
data["albumArtistPersistentID"] = songItem.albumArtistPersistentID | |
// var albumPersistentID: MPMediaEntityPersistentID | |
// The persistent identifier for an album. | |
data["albumPersistentID"] = songItem.albumPersistentID | |
// var albumTitle: String? | |
// The title of an album, such as “Live On Mars”, as opposed to the title of an individual song on the album, such as “Crater Dance (radio edit)”. | |
data["title"] = songItem.title ?? "" | |
// var albumTrackCount: Int | |
// The number of tracks in the album that contains the media item. | |
data["albumTrackCount"] = songItem.albumTrackCount | |
// var albumTrackNumber: Int | |
// The track number of the media item, for a media item that is part of an album. | |
data["albumTrackNumber"] = songItem.albumTrackNumber | |
// var artist: String? | |
// The performing artist(s) for a media item—which may vary from the primary artist for the album that a media item belongs to. | |
data["artist"] = songItem.artist ?? "" | |
// var artistPersistentID: MPMediaEntityPersistentID | |
// The persistent identifier for an artist. | |
data["artistPersistentID"] = songItem.artistPersistentID | |
// var artwork: MPMediaItemArtwork? | |
// The artwork image for the media item. | |
data["artwork"] = { | |
if songItem.artwork == nil { return "" } | |
let image = songItem.artwork!.image(at: /* CGSize(width: 100, height: 100) */ songItem.artwork!.bounds.size) | |
if image == nil { return "" } | |
let png = UIImagePNGRepresentation(image!) | |
if png == nil { return "" } | |
return png!.base64EncodedString() | |
}() | |
// var assetURL: URL? | |
// The URL pointing to the media item. | |
data["assetURL"] = songItem.assetURL != nil ? songItem.assetURL!.absoluteString : "" | |
// var beatsPerMinute: Int | |
// The number of musical beats per minute for the media item. | |
data["beatsPerMinute"] = songItem.beatsPerMinute | |
// var bookmarkTime: TimeInterval (Alias Double) | |
// The user’s place in the media item the most recent time it was played. | |
data["bookmarkTime"] = songItem.bookmarkTime | |
// var isCloudItem: Bool | |
// A Boolean value indicating whether the media item is an iCloud Music Library item. | |
data["isCloudItem"] = songItem.isCloudItem | |
// var comments: String? | |
// Textual information about the media item. | |
data["comments"] = songItem.comments ?? "" | |
// var isCompilation: Bool | |
// A Boolean value indicating whether the media item is part of a compilation. | |
data["isCompilation"] = songItem.isCompilation | |
// var composer: String? | |
// The musical composer for the media item. | |
data["composer"] = songItem.composer ?? "" | |
// var composerPersistentID: MPMediaEntityPersistentID | |
// The persistent identifier for a composer. | |
data["composerPersistentID"] = songItem.composerPersistentID | |
// var dateAdded: Date | |
// The date the item was added to the library. | |
data["dateAdded"] = songItem.dateAdded.timeIntervalSince1970 | |
// var discCount: Int | |
// The number of discs in the album that contains the media item. | |
data["discCount"] = songItem.discCount | |
// var discNumber: Int | |
// The disc number of the media item, for a media item that is part of a multi-disc album. | |
data["discNumber"] = songItem.discNumber | |
// var isExplicitItem: Bool | |
// A Boolean value that indicates whether the item has explicit (adult) lyrics or language. | |
data["isExplicitItem"] = songItem.isExplicitItem | |
// var genre: String? | |
// The musical or film genre of the media item. | |
data["genre"] = songItem.genre ?? "" | |
// var genrePersistentID: MPMediaEntityPersistentID | |
// The persistent identifier for a genre. | |
data["genrePersistentID"] = songItem.genrePersistentID | |
// var lastPlayedDate: Date? | |
// The date a media item was last played. | |
data["lastPlayedDate"] = songItem.lastPlayedDate != nil ? songItem.lastPlayedDate!.timeIntervalSince1970 : 0 | |
// var lyrics: String? | |
// The lyrics for the media item. | |
data["lyrics"] = songItem.lyrics ?? "" as String | |
// var mediaType: MPMediaType | |
// The media type of the media item. | |
data["mediaType"] = { | |
switch songItem.mediaType { | |
// Audio | |
case MPMediaType.music: | |
return "music" | |
case MPMediaType.podcast: | |
return "podcast" | |
case MPMediaType.audioBook: | |
return "audioBook" | |
case MPMediaType.audioITunesU: | |
return "audioITunesU" | |
case MPMediaType.anyAudio: | |
return "anyAudio" | |
// Video | |
case MPMediaType.movie: | |
return "movie" | |
case MPMediaType.tvShow: | |
return "tvShow" | |
case MPMediaType.videoPodcast: | |
return "videoPodcast" | |
case MPMediaType.musicVideo: | |
return "musicVideo" | |
case MPMediaType.videoITunesU: | |
return "videoITunesU" | |
case MPMediaType.homeVideo: | |
return "homeVideo" | |
case MPMediaType.anyVideo: | |
return "anyVideo" | |
// Any | |
default: | |
return "any" | |
} | |
}() | |
// var persistentID: MPMediaEntityPersistentID (Alias UInt64) | |
// The persistent identifier for the media item. | |
data["persistentID"] = songItem.persistentID | |
// var playCount: Int | |
// The number of times the user has played the media item. | |
data["playCount"] = songItem.playCount | |
// var playbackDuration: TimeInterval | |
// The playback duration of the media item. | |
data["playbackDuration"] = songItem.playbackDuration | |
// var playbackStoreID: String | |
// The non-library identifier for a media item. | |
data["playbackStoreID"] = songItem.playbackStoreID | |
// var podcastPersistentID: MPMediaEntityPersistentID | |
// The persistent identifier for an audio podcast. | |
data["podcastPersistentID"] = songItem.podcastPersistentID | |
// var podcastTitle: String? | |
// The title of a podcast, such as “This Martian Drudgery”, as opposed to the title of an individual episode of a podcast such as “Episode 12: Another Cold Day At The Pole”. | |
data["podcastTitle"] = songItem.podcastTitle ?? "" | |
// var hasProtectedAsset: Bool | |
// A Boolean indicating whether the media item has a protected asset. | |
data["hasProtectedAsset"] = songItem.hasProtectedAsset | |
// var rating: Int | |
// The user-specified rating of the object in the range [0...5], where a value of 5 indicates the most favorable rating. | |
data["rating"] = songItem.rating | |
// var releaseDate: Date? | |
// The date on which the media item was first publicly released. | |
data["releaseDate"] = songItem.releaseDate != nil ? songItem.releaseDate!.timeIntervalSince1970 : 0 | |
// var skipCount: Int | |
// The number of times the user has skipped playing the item. | |
data["skipCount"] = songItem.skipCount | |
// var title: String? | |
// The title (or name) of the media item. | |
data["title"] = songItem.title ?? "" | |
// var userGrouping: String? | |
// Grouping information for the media item. | |
data["userGrouping"] = songItem.userGrouping ?? "" | |
// Output | |
songs.append( data ) | |
} | |
let result = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: songs) | |
self.commandDelegate!.send(result, callbackId: command.callbackId) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment