简体   繁体   English

从音乐库中获取歌曲列表后如何播放音乐文件?

[英]How to play music file after fetching list of songs from Music Library?

In my application i have to make music library. 在我的应用程序中,我必须制作音乐库。 So for that i have to fetch songs from Music Library. 所以我必须从音乐库中获取歌曲。 I done till this bye following. 我完成了直到这个再见。

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];

than display it in table view. 而不是在表视图中显示它。

when i have to play song on selected index i fetch song name but don`t know how to play that song, 当我必须在所选索引上播放歌曲时,我会获取歌曲名称但不知道如何播放该歌曲,

- (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];
}

but my sound file not going to play... 但我的声音文件不会播放...

Hey any one know about this than help me.. 嘿,任何人都知道这个比帮助我..

Thanks AJPatel 谢谢AJPatel

You are not storing a URL in your array, you are storing instances of MPMediaItem 您没有在数组中存储URL,而是存储MPMediaItem实例

What you need to do is extract the assets url from the media item. 您需要做的是从媒体项中提取资源URL。 You can do this as follows ... 你可以这样做......

MPMediaItem *item = [appDeleg.arryMusic objectAtIndex:indexPath.row];
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
[[self avPlayer] replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithURL:url]];
[self.avPlayer play];

You have to initialize a player object(appdelegate will be a good position) 你必须初始化一个玩家对象(appdelegate将是一个好位置)

        MPMusicPlayerController *musicPlayer; //in .h file
  self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
 [self.musicPlayer setRepeatMode:MPMusicRepeatModeNone];
 [self.musicPlayer setShuffleMode:MPMusicShuffleModeOff];
 [self.musicPlayer beginGeneratingPlaybackNotifications];

Then have to maintain an array for holding media elements 然后必须维护一个用于保存媒体元素的数组

MPMediaItemCollection *userMediaItemCollection;// in .h file @property (nonatomic, retain) MPMediaItemCollection *userMediaItemCollection; MPMediaItemCollection * userMediaItemCollection; //在.h文件中@property(非原子,保留)MPMediaItemCollection * userMediaItemCollection; //in .h file //在.h文件中

You have to add your media item to the array(input list) and add that to the mediacollection list 您必须将媒体项添加到阵列(输入列表)并将其添加到mediacollection列表

[self setUserMediaItemCollection: [MPMediaItemCollection collectionWithItems: (NSArray *) inputList]];

Add your media collection list to the player 将媒体收藏列表添加到播放器

[APP_DELEGATE.musicPlayer setQueueWithItemCollection: self.userMediaItemCollection];

Now play 现在玩

[APP_DELEGATE.musicPlayer play];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM