如何使用MPMediaItemPropertyPersistentID在iPhone音乐播放器框架中播放音乐? [英] How do you use MPMediaItemPropertyPersistentID to play music in iPhone Music Player Framework?
本文介绍了如何使用MPMediaItemPropertyPersistentID在iPhone音乐播放器框架中播放音乐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码成功地为我的整个音乐库编目歌曲名称和ID。但是,它实际上不会使用此方法播放歌曲,并且控制台显示以下内容:
My code sucessfully catalogs song names and ID's for my the entire music library. However, it will not actually play a song using this methodology and the console displays the following:
消息播放状态超时。
消息nowPlayingItem超时。
Message nowPlayingItem timed out.
self.musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSArray *itemsFromGenericQuery = [everything items];
SongName = [[NSMutableArray alloc] init];
SongItem = [[NSMutableArray alloc] init];
NSString *songTitle;
NSString *songID;
//Collect names & ID for entire music library & put into arrays
for (MPMediaItem *song in itemsFromGenericQuery) {
songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
[SongName addObject:songTitle];
songID = [song valueForProperty: MPMediaItemPropertyPersistentID];
[SongItem addObject:songID];
}
NSLog (@"%@", [SongName objectAtIndex:1]);
NSLog (@"%@", [SongItem objectAtIndex:1]);
// Play the second song in the list
MPMediaItemCollection *collection = [MPMediaItemCollection collectionWithItems:[NSArray arrayWithObject:[SongItem objectAtIndex:1]]];
[self.musicPlayer setQueueWithItemCollection:collection];
[self.musicPlayer play];
推荐答案
我将再一次回答我自己的问题。问题是collectionWithItems:需要一个MPMediaItems数组,而不是MPMediaItemPropertyPersistentIDs数组。以下是可能遇到同样问题的人的工作代码:
Once again, I'll answer my own question. The issue was that collectionWithItems: expects an array of MPMediaItems, not an array of MPMediaItemPropertyPersistentIDs. Here is the working code for anyone who may have the same problem:
MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSArray *itemsFromGenericQuery = [everything items];
SongItem = [[NSMutableArray alloc] init];
for (MPMediaItem *song in itemsFromGenericQuery) {
NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
//NSLog (@"%@", songTitle);
songID = [song valueForProperty: MPMediaItemPropertyPersistentID];
//NSLog (@"%@", songID);
[SongItem addObject:songID];
}
//Choose the first indexed song
NSString *selectedTitle = [SongItem objectAtIndex:0];
//Use the MPMediaItemPropertyPersistentID to play the song
MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:selectedTitle forProperty:MPMediaItemPropertyPersistentID];
MPMediaQuery *mySongQuery = [[MPMediaQuery alloc] init];
[mySongQuery addFilterPredicate: predicate];
[musicPlayer setQueueWithQuery:mySongQuery];
[musicPlayer play];
这篇关于如何使用MPMediaItemPropertyPersistentID在iPhone音乐播放器框架中播放音乐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文