-1

I am able to access the music albums that I've bought from iTunes using following code.

MPMediaQuery * query = [MPMediaQuery albumsQuery];
allAlbums = query.collections;
return allAlbums;

But Not able to list the Music albums which are manually added from my PC. Itunes is able to show them in added recently. But my app is not displaying them?

How to access manually added music files?

Eric Aya
  • 69,000
  • 34
  • 174
  • 243
AMI amitekh
  • 188
  • 3
  • 16

1 Answers1

0

Try Following:

First import the AVFoundation/AVFoundation.h framework.

#import <AVFoundation/AVFoundation.h>
-(void)pickAudioFiles
{
    MPMediaPickerController *soundPicker=[[MPMediaPickerController alloc] 
       initWithMediaTypes:MPMediaTypeAnyAudio];
    soundPicker.delegate=self;
    soundPicker.allowsPickingMultipleItems=NO; // You can set it to yes for multiple selection
    [self presentViewController:soundPicker animated:YES completion:nil];
 }

 -(void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:
(MPMediaItemCollection *)mediaItemCollection
{
   MPMediaItem *item = [[mediaItemCollection items] objectAtIndex:0]; // For multiple you can iterate iTems array
   NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
   [mediaPicker dismissViewControllerAnimated:YES completion:nil];
   AVPlayerItem *playerItem=[AVPlayerItem playerItemWithURL:url];
   AVPlayer *player=[[AVPlayer alloc] initWithPlayerItem:playerItem];
   AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:player];
   playerLayer.frame=CGRectMake(0, 0, 10, 10);
   [self.view.layer addSublayer:playerLayer];
}

You can play with

 [player play]

Note : If you want to load all data at once please look at following link. This might help you in all at once case.

Fetch All Audio & Video files in ios

Community
  • 1
  • 1
MOHAMMAD ISHAQ
  • 948
  • 7
  • 14