How to extract MPMediaItemCollection to NSData in AVPlayer?

1.1k views Asked by At

I want to play device songs using avplayer in my app. In NSLog i m getting array value ===> " 18023427656013653237", " 15946441828903552798", " 10791915036547881152", " 17986655796364263078" // Now i want to what are these values .....

I have tried this.....

-(IBAction)SelectSongs:(id)sender
{

MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAny];
picker.delegate = self;
picker.allowsPickingMultipleItems =YES;
picker.prompt = @"Select songs from the list";

[self presentViewController:picker animated:YES completion:^(void) {}];
}

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


if (mediaItemCollection)
{
    NSMutableArray *medialibraryArray;
    medialibraryArray=[[NSMutableArray alloc]init];
    [medialibraryArray addObject:mediaItemCollection.items];// Modified
    NSLog(@"%@=========>",medialibraryArray);


}
[self dismissViewControllerAnimated:YES completion:^(void) {}];}

-(void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker
{
[self dismissViewControllerAnimated:YES completion:^(void) {}];
}


// Thanks in Advance
1

There are 1 answers

1
amergin On BEST ANSWER
-(void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems:  (MPMediaItemCollection *) mediaItemCollection
{


    if (mediaItemCollection)
    {
        NSArray *items = [mediaItemCollection items];
        for( int n=0; n < items.count; n++ )    {
            MPMediaItem *mediaItem = [items objectAtIndex:n];
            NSLog(@"%@=========>",[mediaItem valueForProperty:MPMediaItemPropertyTitle]);
        }

        MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
        [musicPlayer setQueueWithItemCollection: mediaItemCollection];
        NSLog(@"==>%@",musicPlayer);


    }
    [self dismissViewControllerAnimated:YES completion:^(void) {}];
}

You're going to get all the songs in your library with this so the printout might take a while! Read up on MPMediaItem Class Reference and media queries to see how to get a subset of your entire music library.