does any of you have example code (or a link to it) of how to retrieve all music albums or artist from the iPod media library?
Thanks in advance!
Use a MPMediaQuery:
MPMediaQuery *allAlbumsQuery = [MPMediaQuery albumsQuery];
NSArray *allAlbumsArray = [allAlbumsQuery collections];
The allItems array does now contain MPMediaItemCollections, grouping is done by album. Now you can walk through the arrays.
for (MPMediaItemCollection *collection in allAlbumsArray) {
MPMediaItem *item = [collection representativeItem];
}
Thanks for the answer, here is working sample code that prints out the albums and artists in case someone needs it:
NSMutableString *outText = [[NSMutableString alloc] initWithString:#"Albums:"];
[outText appendFormat:#"\r\n count:%i",[[[MPMediaQuery albumsQuery] collections] count]];
for (MPMediaItemCollection *collection in [[MPMediaQuery albumsQuery] collections]) {
[outText appendFormat:#"\r\n -%#",[[collection representativeItem] valueForProperty:MPMediaItemPropertyAlbumTitle]];
}
[outText appendString:#"\r\n\r\n Artist:"];
for (MPMediaItemCollection *collection in [[MPMediaQuery artistsQuery] collections]) {
[outText appendFormat:#"\r\n -%#",[[collection representativeItem] valueForProperty:MPMediaItemPropertyArtist]];
}
NSLog(#"%#",[outText autorelease]);
Here you go. You can get the albums and their songs.
/// Get all albums and their songs
///
func getAllAlbums() {
let query: MPMediaQuery = MPMediaQuery.albums()
let allAlbums = query.collections
allAlbumItems?.removeAll()
guard allAlbums != nil else {
return
}
for collection in allAlbums! {
let item: MPMediaItem? = collection.representativeItem
let albumName = item?.value(forKey: MPMediaItemPropertyAlbumTitle) as? String ?? "<Unknown>"
let albumId = item!.value(forProperty: MPMediaItemPropertyAlbumPersistentID) as! NSNumber
let artistName = item?.value(forKey: MPMediaItemPropertyArtist) as? String ?? "<Unknown>"
let album = Album()
album.name = albumName
album.artistName = artistName
album.albumId = String(describing: albumId)
print("Album name: \(albumName)")
// Get all songs in this album
let mediaQuery = MPMediaQuery.songs()
let predicate = MPMediaPropertyPredicate.init(value: albumId, forProperty: MPMediaItemPropertyAlbumPersistentID)
mediaQuery.addFilterPredicate(predicate)
let song = mediaQuery.items
if let allSongs = song {
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
album.songs?.append(trackInfo)
index += 1
}
}
}
// Finally add the album object to albums array
allAlbumItems?.append(album)
}
print("Total Album count: \(allAlbumItems?.count)")
}
Related
I want to pull details from local database but when I do the necessary operations, it returns null as a result. I can't understand where I went wrong.
var chosenCar=""
var chosenCarId : UUID?
the arrays I created, I transfer data to these arrays, there is no problem with that, I did a test
if chosenCar != "" {
//core data
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "CarsInformation")
let idString = chosenCarId?.uuidString
fetchRequest.predicate = NSPredicate(format: "id = %#", idString!)
fetchRequest.returnsObjectsAsFaults = false
do {
let results = try context.fetch(fetchRequest)
if results.count > 0 {
for result in results as! [NSManagedObject] {
if let name = result.value(forKey: "models") as? String {
modelTextField.text = name
}
if let year = result.value(forKey: "year") as? Int {
yearTextField.text = String(year)
}
if let price = result.value(forKey: "price") as? Int {
priceTextField.text = String(price)
}
if let imageData = result.value(forKey: "image") as? Data {
let image = UIImage(data: imageData)
imageView.image = image
}
}
}
} catch{
print("error")
}
} else {
modelTextField.text = ""
priceTextField.text = ""
yearTextField.text = ""
}
After doing these operations, I can't get the result I want.
this is my respone
{
activeFromDate = "01/01/2017";
terminateDate = “02/05/2019”;
}
{
activeFromDate = "01/01/2013";
terminateDate = "<null>";
}
{
activeFromDate = "01/01/2017";
terminateDate = "02/05/2018";
}
{
activeFromDate = "07/01/2012";
terminateDate = "<null>";
}
{
activeFromDate = "01/01/2017";
terminateDate = "02/05/2019";
}
this is my code
let answerArray = NSMutableArray()
for i in 0..<arr.count
{
let dict = Dictionary[i] as! NSDictionary
let name1 = dict.value(forKey:"terminateDate") as! String
if(name1 == "")
{
print (answerArray.add(dict))
}
}
this is my nsdictionary response
how to get array count if terminatedDate is null and terminatedDate is greater than today date?
I have created a sample to read json file placed in your project. and have integrated the code as per your requirement.
Json File
{"Status":"success","Data":[
{"activeFromDate":"01/01/2017","terminateDate":"02/05/2019"},
{"activeFromDate":"01/01/2013","terminateDate":null},
{"activeFromDate":"01/01/2017","terminateDate":"02/05/2018"},
{"activeFromDate":"07/01/2012","terminateDate":null},
{"activeFromDate":"01/01/2017","terminateDate":"02/05/2016"},
{"activeFromDate":"01/01/2017","terminateDate":"02/05/2019"}
]
}
In your View Controller
import UIKit
class HomeViewController: UIViewController {
let answerArray = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
jsonParsingFromFile()
}
func jsonParsingFromFile()
{
let path: NSString = Bundle.main.path(forResource: "tjw", ofType: "json")! as NSString
let data : NSData = try! NSData(contentsOfFile: path as String, options: NSData.ReadingOptions.dataReadingMapped)
self.startParsing(data: data)
}
func startParsing(data :NSData)
{
let dict: NSDictionary!=(try! JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.mutableContainers)) as? NSDictionary
//print(dict)
guard let dataArr:NSArray = dict.value(forKey: "Data") as? NSArray else {
return
}
print(dataArr.count)
for i in 0..<dataArr.count{
guard let currentDict:NSDictionary = dataArr[i] as? NSDictionary else {
return
}
let activeFromDateStr:String = currentDict.value(forKey: "activeFromDate") as? String ?? ""
let terminateDateStr:String = currentDict.value(forKey: "terminateDate") as? String ?? ""
print(activeFromDateStr, terminateDateStr)
if terminateDateStr != ""{
let date2 = convertToDate(dateStr: terminateDateStr)
let today = Date()
if date2>today{
answerArray.add(currentDict)
}
}else{
answerArray.add(currentDict)
}
}
print(answerArray)
}
func convertToDate(dateStr:String)->Date{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
dateFormatter.locale = Locale(identifier: "en_IN")
let date = dateFormatter.date(from: dateStr)!
return date
}
}
I was looking other stack overflow questions but it seems like it doesn't work for my case.
I'm making YouTube app and my code is below.
if let JSON = response.result.value as? NSDictionary{
var arrayOfVideos = [Video]()
for video in JSON["items"] as! NSArray{
print(video)
//Create video onjects off of the JSON response
let videoObj = Video()
//I got errors following part
videoObj.videoId = video.valueForKeyPath("snippet.resourceId.videoId") as! String
videoObj.videoTitle = video.valueForKeyPath("snippet.title") as! String
videoObj.videoDescription = video.valueForKeyPath("snippet.description") as! String
videoObj.videoThumbnailUrl = video.valueForKeyPath("snippet.thumbnails.maxres.url") as! String
arrayOfVideos.append(videoObj)
}
self.videoArray = arrayOfVideos
}else{
print("couldn't get video information")
}
I have also a class Video which define title, description and thumbnail.
class Video: NSObject {
var videoId:String = ""
var videoTitle:String = ""
var videoDescription:String = ""
var videoThumbnailUrl:String = ""
}
And I got these errors.
Value of type 'Any' has no member 'valueForKeyPath'
And also this
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
I appreciate any help. Thank you in advance.
You can try
if let JSON = response.result.value as? [String:Any] {
var arrayOfVideos = [Video]()
if let videos = JSON["items"] as? [[String:Any]] {
for video in videos {
let videoObj = Video()
if let videoId = video["snippet.resourceId.videoId"] as? String {
videoObj.videoId = videoId
}
if let videoTitle = video["snippet.title"] as? String {
videoObj.videoTitle = videoTitle
}
if let videoDescription = video["snippet.description"] as? String {
videoObj.videoDescription = videoDescription
}
if let videoThumbnailUrl = video["snippet.thumbnails.maxres.url"] as? String {
videoObj.videoThumbnailUrl = videoThumbnailUrl
}
arrayOfVideos.append(videoObj)
}
}
}
I need convert NSArray to Dictionary, but don't know how can I do it.
After fetch request I have result in NSArray. This my request:
var results: NSArray = []
func fetchUpdateAttendee() {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Attendee")
let predicate = NSPredicate(format: "needUpdate != false")
fetchRequest.predicate = predicate
results = try! DBWorker.context.fetch(fetchRequest) as NSArray
results.forEach { result in
print(result)
}
var results: NSArray = []
print(results) give me
<Attendee: 0x60c000097750> (entity: Attendee; id: 0x60c000422200 <x-coredata:///Attendee/t9E88E2EE-9258-4FAE-AF80-9B036838C6D631> ; data: {
address = "1411 E 31st St";
affiliation = "";
attendeeType = nil;
city = Aguanga;
degree = MD;
email = "";
fax = "";
firstName = Oliver1212;
fullStateLicense = "";
id = nil;
lastName = Aalami;
meeting = nil;
needUpdate = 1;
phone = "";
signature = nil;
signatureTimeStamp = nil;
specialty = Surgery;
state = CA;
stateLicense = "";
status = nil;
timeStamp = nil;
zip = 92536;
})
I need to put these datas to: let dic4Attendee: [String: Any] = [:]
This is a Core Data NSManagedObject subclass, so the code is supposed to be
var results = [Attendee]()
func fetchUpdateAttendee() {
let fetchRequest = NSFetchRequest<Attendee>(entityName: "Attendee")
let predicate = NSPredicate(format: "needUpdate == TRUE")
fetchRequest.predicate = predicate
do {
results = try DBWorker.context.fetch(fetchRequest)
results.forEach { result in
print(result)
}
} catch { print(error) }
}
never use NSArray to represent an array of NSManagedObject subclass.
Add a property dictionaryRepresentation in the Attendee class and return the key value pairs you need for example
var dictionaryRepresentation : [String:Any] {
return ["address" : address,
"affiliation" : affiliation,
"city" : city
// and so on
]
}
then map the array
let mappedArray = results.map{ $0.dictionaryRepresentation }
That's my Question =)
MPMediaQuery *artistQuery = [MPMediaQuery artistsQuery];
NSArray *songsByArtist = [artistQuery collections];
How can I get the number of albums of each artists of the MPMediaItemCollections in songsByArtist ?
Exemple :
The Beatles
3 Albums
AC/DC
6 Albums
Thank You !!
I get number of albums and songs for artist using predicate:
MPMediaPropertyPredicate *artistNamePredicate = [MPMediaPropertyPredicate predicateWithValue:#"ArtistName" forProperty:MPMediaItemPropertyArtist];
MPMediaQuery *myComplexQuery = [[MPMediaQuery alloc] init];
[myComplexQuery addFilterPredicate: artistNamePredicate];
NSInteger songCount = [[myComplexQuery collections] count]; //number of songs
myComplexQuery.groupingType = MPMediaGroupingAlbum;
NSInteger albumCount = [[myComplexQuery collections] count]; //number of albums
The artistsQuery convenience constructor does not sort and group by album. artistsQuery returns an array of media item collections of all artists sorted alphabetically by artist name. Nested inside each artist collection is an array of media items associated with all songs for that artist. The nested array is sorted alphabetically by song title.
One way to keep a count of albums by artist is to enumerate through all the song items for each artist collection and use a NSMutableSet to keep track of distinct album titles associated with each song. Then add the count of the set as the value for each artist key in a NSMutableDictionary. Any duplicate album titles will not be added since a NSMutableSet will only take distinct objects:
MPMediaQuery *artistQuery = [MPMediaQuery artistsQuery];
NSArray *songsByArtist = [artistQuery collections];
NSMutableDictionary *artistDictionary = [NSMutableDictionary dictionary];
NSMutableSet *tempSet = [NSMutableSet set];
[songsByArtist enumerateObjectsUsingBlock:^(MPMediaItemCollection *artistCollection, NSUInteger idx, BOOL *stop) {
NSString *artistName = [[artistCollection representativeItem] valueForProperty:MPMediaItemPropertyArtist];
[[artistCollection items] enumerateObjectsUsingBlock:^(MPMediaItem *songItem, NSUInteger idx, BOOL *stop) {
NSString *albumName = [songItem valueForProperty:MPMediaItemPropertyAlbumTitle];
[tempSet addObject:albumName];
}];
[artistDictionary setValue:[NSNumber numberWithUnsignedInteger:[tempSet count]]
forKey:artistName];
[tempSet removeAllObjects];
}];
NSLog(#"Artist Album Count Dictionary: %#", artistDictionary);
It would be cleaner if you change the query to albumsQuery. This query groups and sorts the collection by album name. Then it is just a matter of enumerating through the array of album collections and keeping a count of the representative artist name for each album in a NSCountedSet. The counted set will track the number of times objects are inserted:
MPMediaQuery *albumQuery = [MPMediaQuery albumsQuery];
NSArray *albumCollection = [albumQuery collections];
NSCountedSet *artistAlbumCounter = [NSCountedSet set];
[albumCollection enumerateObjectsUsingBlock:^(MPMediaItemCollection *album, NSUInteger idx, BOOL *stop) {
NSString *artistName = [[album representativeItem] valueForProperty:MPMediaItemPropertyArtist];
[artistAlbumCounter addObject:artistName];
}];
NSLog(#"Artist Album Counted Set: %#", artistAlbumCounter);
You can also retrieve the count for a given object in a NSCountedSet with the countForObject: method.
Swift 2 translation of Bryan's answer:
var artistQuery = MPMediaQuery.artistsQuery()
var artistQuery.groupingType = MPMediaGrouping.AlbumArtist
var songsByArtist = artistQuery.collections
var artistDictionary = NSMutableDictionary()
var tempSet = NSMutableSet()
songsByArtist.enumerateObjectsUsingBlock { (artistCollection, idx, stop) -> Void in
let collection = artistCollection as! MPMediaItemCollection
let rowItem = collection.representativeItem
let artistName = rowItem?.valueForProperty(MPMediaItemPropertyAlbumArtist)
let collectionContent:NSArray = collection.items
collectionContent.enumerateObjectsUsingBlock { (songItem, idx, stop) -> Void in
let item = songItem as! MPMediaItem
let albumName = item.valueForProperty(MPMediaItemPropertyAlbumTitle)
self.tempSet.addObject(albumName!)
}
self.artistDictionary.setValue(NSNumber(unsignedInteger: self.tempSet.count), forKey: artistName! as! String)
self.tempSet.removeAllObjects()
}
print("Album Count Dictionary: \(artistDictionary)")
albumsQuery us this if it helps u or see more on this link below
http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMediaQuery_ClassReference/Reference/Reference.html
Thanks Tim E, I couldn't get your code to work first time, but I modded it to this and it works now.
let artistQuery = MPMediaQuery.artistsQuery()
artistQuery.groupingType = MPMediaGrouping.AlbumArtist
let songsByArtist = artistQuery.collections! as NSArray
let artistDictionary = NSMutableDictionary()
let tempSet = NSMutableSet()
songsByArtist.enumerateObjectsUsingBlock( { (artistCollection, idx, stop) -> Void in
let collection = artistCollection as! MPMediaItemCollection
let rowItem = collection.representativeItem
let artistName = rowItem?.valueForProperty(MPMediaItemPropertyAlbumArtist)
let collectionContent:NSArray = collection.items
collectionContent.enumerateObjectsUsingBlock({ (songItem, idx, stop) -> Void in
let item = songItem as! MPMediaItem
let albumName = item.valueForProperty(MPMediaItemPropertyAlbumTitle)
tempSet.addObject(albumName!)
})
artistDictionary.setValue(NSNumber(unsignedInteger: UInt(tempSet.count)), forKey: artistName! as! String)
tempSet.removeAllObjects()
})
print("Album Count Dictionary: \(artistDictionary)")
Sorry for late answer.
Posting my answer in case it is helpful to someone.
Below code gets all artists group by album artist and get its all Albums and songs inside the album.
/// Get all artists and their songs
///
func getAllArtists() {
let query: MPMediaQuery = MPMediaQuery.artists()
query.groupingType = .albumArtist
let artistsColelctions = query.collections
artists.removeAll()
var tempSet = NSMutableSet()
guard artistsColelctions != nil else {
return
}
// 1. Create Artist Objects from collection
for collection in artistsColelctions! {
let item: MPMediaItem? = collection.representativeItem
var artistName = item?.value(forKey: MPMediaItemPropertyArtist) as? String ?? "<Unknown>"
artistName = artistName.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
let artistId = item!.value(forProperty: MPMediaItemPropertyArtistPersistentID) as! NSNumber
// temp
let albumName = item?.albumTitle
let albumID = item?.albumPersistentID
print(albumName)
print(albumID)
// Create artist item
let artist = Artist()
artist.name = artistName
artist.artworkTitle = String(artistName.characters.prefix(1)).uppercased()
artist.artistId = String(describing: artistId)
// 2. Get Albums for respective Artist object
//--------------------------------------------
let mediaQuery2 = MPMediaQuery.albums()
let predicate2 = MPMediaPropertyPredicate.init(value: artistId, forProperty: MPMediaItemPropertyArtistPersistentID)
mediaQuery2.addFilterPredicate(predicate2)
let albums = mediaQuery2.collections
for collection in albums! {
let item: MPMediaItem? = collection.representativeItem
let albumName = item?.value(forKey: MPMediaItemPropertyAlbumTitle) as? String ?? "<Unknown>"
let albumId = item!.value(forProperty: MPMediaItemPropertyAlbumPersistentID) as! NSNumber
let artistName = item?.value(forKey: MPMediaItemPropertyAlbumArtist) as? String ?? "unknown"
let genreName = item?.genre ?? ""
let year = item?.releaseDate ?? item?.dateAdded
let dateAdded = item?.dateAdded
// Create album object
let album = Album()
album.name = albumName
album.artistName = artistName
album.genre = genreName
album.releaseDate = year
album.dateAdded = dateAdded
album.albumId = String(describing: albumId)
// Add artwork to album object
let artwork = Artwork.init(forAlbum: item)
album.artwork = artwork
// 3. Get Songs inside the resepctive Album object
//---------------------------------------------------
let mediaQuery = MPMediaQuery.songs()
let predicate = MPMediaPropertyPredicate.init(value: albumId, forProperty: MPMediaItemPropertyAlbumPersistentID)
mediaQuery.addFilterPredicate(predicate)
let song = mediaQuery.items
if let allSongs = song {
var index = 0
for item in allSongs {
let pathURL: URL? = item.value(forProperty: MPMediaItemPropertyAssetURL) as? URL
let isCloud = item.value(forProperty: MPMediaItemPropertyIsCloudItem) as! Bool
let trackInfo = TrackInfo()
trackInfo.index = index
trackInfo.mediaItem = item
trackInfo.isCloudItem = isCloud
trackInfo.isExplicitItem = item.isExplicitItem
trackInfo.isSelected = false
trackInfo.songURL = pathURL
album.songs?.append(trackInfo)
index += 1
}
}
artist.albums?.append(album)
}
// Finally add the artist object to Artists Array
artists.append(artist)
}
}