0

I get Song title in TableView using this code:

MPMediaQuery *everything = [[MPMediaQuery alloc] init];

    NSArray *itemsFromGenericQuery = [everything items];

    for (MPMediaItem *song in itemsFromGenericQuery) {

        NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
        [songTitleArray addObject:songTitle];
    }

Now I want to play that song inside tableView I added Two button play and Stop how to play using Title in same ViewController.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Mad Burea
  • 531
  • 8
  • 22
  • Hope this will helpful: http://stackoverflow.com/questions/13352767/mpmediaquery-search-for-artists-albums-and-songs – Amanpreet Dec 09 '16 at 10:09
  • @Amanpreet At this link it has been stated that How to get Title of Song. i have already fetched Title. Now want to play. – Mad Burea Dec 09 '16 at 10:11

1 Answers1

0

To play song you can do something like :

MPMusicPlayerController *controller = [MPMusicPlayerController iPodMusicPlayer];

MPMediaItemCollection *collection = [[MPMediaItemCollection alloc] initWithItems:arrayOfMediaItems];
MPMediaItem *item = [collection representativeItem];

[controller setQueueWithItemCollection:collection];
[controller setNowPlayingItem:item];

[controller prepareToPlay];
[controller play];

You can also use AVPlayer to play song, after picking it through MPMediaPickerController :

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

}
Munahil
  • 2,381
  • 1
  • 14
  • 24
  • what to write in arrayOfMediaItems. i got the Title of Song from My snippets. is there is any way to play song based on my Song Title. My array having only Song Title. – Mad Burea Dec 09 '16 at 10:14
  • i don't want Present MPMediaPickerController. so this delegates method didPickMediaItems having no use – Mad Burea Dec 09 '16 at 10:16
  • Why don't you directly play "song", instead of getting its title and saving in "songTitleArray". Play it directly. – Munahil Dec 09 '16 at 10:16
  • How? User will get idea about Which Song it is. it's in My tableview and want to play from that ViewController not from MPMediaPickerController – Mad Burea Dec 09 '16 at 10:20
  • You can display its name in the tableview like, but to play the song you will need this "MPMediaItem", you can not play with string only – Munahil Dec 09 '16 at 10:22
  • Ok So i have that Title Now when one click on certain title then how will it play. as you have written code in didPickMediaItems that is Delegate. in my case this will be no there – Mad Burea Dec 09 '16 at 10:25
  • Instead of making an array of "songTitles", you can show this title from "itemsFromGenericQuery" array, like : "[itemsFromGenericQuery.objectAtIndex:indexPath.row].valueForProperty(MPMediaItemPropertyTitle)" and to play use "[controller setNowPlayingItem:itemsFromGenericQuery.objectAtIndex:indexPath.row]; [controller prepareToPlay]; [controller play];" – Munahil Dec 09 '16 at 10:32