在我的应用程序中,我必须制作音乐库。所以我必须从音乐库中获取歌曲。我完成了直到这个再见。
Test_MusicLibraryAppDelegate *appDeleg = (Test_MusicLibraryAppDelegate *)[[UIApplication sharedApplication]delegate];
//NSMutableDictionary *musicList = [NSMutableDictionary dictionary];
MPMediaQuery *query = [[MPMediaQuery alloc] init];
[query addFilterPredicate:[MPMediaPropertyPredicate
predicateWithValue:[NSNumber numberWithInteger:(MPMediaTypeMusic)]
forProperty:MPMediaItemPropertyMediaType]];
for (MPMediaItem* item in [query items]) {
[appDeleg.arryMusic addObject:item];
}
NSLog(@"Count :: %d ",[appDeleg.arryMusic count]);
[query release];
而不是在表格视图中显示它。
当我必须在所选索引上播放歌曲时,我会获取歌曲名称但不知道如何播放该歌曲,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Test_MusicLibraryAppDelegate *appDeleg = (Test_MusicLibraryAppDelegate *)[[UIApplication sharedApplication]delegate];
// MPMediaItem *song = [appDeleg.arryMusic objectAtIndex:indexPath.row];
NSURL *aURL = [appDeleg.arryMusic objectAtIndex:indexPath.row];
//[song valueForProperty:MPMediaItemPropertyAssetURL];
[[self avPlayer] replaceCurrentItemWithPlayerItem:
[AVPlayerItem playerItemWithURL:aURL]];
[avPlayer play];
}
但我的声音文件无法播放...
嘿,任何人都知道这件事,而不是帮助我......
由于 AJPatel
答案 0 :(得分:6)
您没有在数组中存储URL,而是存储MPMediaItem
您需要做的是从媒体项中提取资源网址。你可以这样做......
MPMediaItem *item = [appDeleg.arryMusic objectAtIndex:indexPath.row];
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
[[self avPlayer] replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithURL:url]];
[self.avPlayer play];
答案 1 :(得分:0)
你必须初始化一个玩家对象(appdelegate将是一个好位置)
MPMusicPlayerController *musicPlayer; //in .h file
self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
[self.musicPlayer setRepeatMode:MPMusicRepeatModeNone];
[self.musicPlayer setShuffleMode:MPMusicShuffleModeOff];
[self.musicPlayer beginGeneratingPlaybackNotifications];
然后必须维护一个用于存放媒体元素的数组
MPMediaItemCollection * userMediaItemCollection; //在.h文件中 @property(nonatomic,retain)MPMediaItemCollection * userMediaItemCollection; //在.h文件中
您必须将媒体项添加到数组(输入列表)并将其添加到mediacollection列表
[self setUserMediaItemCollection: [MPMediaItemCollection collectionWithItems: (NSArray *) inputList]];
将您的媒体收藏列表添加到播放器
[APP_DELEGATE.musicPlayer setQueueWithItemCollection: self.userMediaItemCollection];
现在播放
[APP_DELEGATE.musicPlayer play];