使用mpmediaitempropertytitle播放歌曲



我使用此代码在表格中获得歌曲标题:

MPMediaQuery *everything = [[MPMediaQuery alloc] init];
    NSArray *itemsFromGenericQuery = [everything items];
    for (MPMediaItem *song in itemsFromGenericQuery) {
        NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
        [songTitleArray addObject:songTitle];
    }

现在我想在tableview中播放那首歌,我添加了两个按钮播放,然后停止在同一viewController中使用标题播放。

播放歌曲,您可以做类似的事情:

MPMusicPlayerController *controller = [MPMusicPlayerController iPodMusicPlayer];
MPMediaItemCollection *collection = [[MPMediaItemCollection alloc] initWithItems:arrayOfMediaItems];
MPMediaItem *item = [collection representativeItem];
[controller setQueueWithItemCollection:collection];
[controller setNowPlayingItem:item];
[controller prepareToPlay];
[controller play];

通过MPMediaPickerController挑选之后,您也可以使用AVPlayer播放歌曲:

- (void) mediaPicker: (MPMediaPickerController *) mediaPicker
   didPickMediaItems: (MPMediaItemCollection *) collection {

    MPMediaItem *item = [[collection items] objectAtIndex:0];
    NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
    [self dismissModalViewControllerAnimated: YES];
    // Play the item using MPMusicPlayer
    MPMusicPlayerController* appMusicPlayer = [MPMusicPlayerController applicationMusicPlayer];
    [appMusicPlayer setQueueWithItemCollection:collection];
    [appMusicPlayer play];

    // Play the item using AVPlayer
    AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
    AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
    [player play];  
}

相关内容

  • 没有找到相关文章

最新更新