4

I want to access music files which are available on the iPhone and get it listed (or) get the file into my iPhone application some delegats and start playing it. Is it possible to do it ? Similar to how we access images from device photo album using UIImagePickerController delegate methods.

Thank you!

Till
  • 27,559
  • 13
  • 88
  • 122
Getsy
  • 4,887
  • 16
  • 78
  • 139

3 Answers3

4
-(void)loadDeviceMusic{
     MPMediaQuery *everything = [[MPMediaQuery alloc] init];
    [everything addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithBool:NO] forProperty:MPMediaItemPropertyIsCloudItem]];
    NSArray *itemsFromGenericQuery = [everything items];
    for (MPMediaItem *song in itemsFromGenericQuery) {
    NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
    AVAsset *asset = [AVAsset assetWithURL:assetURL];
    NSLog(@"SONGS URL=%@",assetURL);
    if ([asset hasProtectedContent] == NO) {
        MP3ObjectsClass *objMp3=[[MP3ObjectsClass alloc] init];
        objMp3.mp3Url=[song valueForProperty:MPMediaItemPropertyAssetURL];

        objMp3.mp3Duration=[song valueForProperty: MPMediaItemPropertyPlaybackDuration];

        if([song valueForProperty: MPMediaItemPropertyTitle]){
            objMp3.mp3Name=[song valueForProperty: MPMediaItemPropertyTitle];
        }else{
            objMp3.mp3Name=@"Unknown";
        }
        if([song valueForProperty: MPMediaItemPropertyArtist]){
            objMp3.mp3ArtistName=[song valueForProperty: MPMediaItemPropertyArtist];
        }else{
            objMp3.mp3ArtistName=@"Unknown";
        }
        if([song valueForProperty: MPMediaItemPropertyAlbumTitle]){
            objMp3.mp3AlbumTitle=[song valueForProperty: MPMediaItemPropertyAlbumTitle];
        }else{
            objMp3.mp3AlbumTitle=@"Unknown";
        }
        UIImage *mp3Image=[self getAlbumnArtWorkImage:assetURL];
        if(mp3Image){
            objMp3.mp3Image=mp3Image;
        }else{
            objMp3.mp3Image=[UIImage imageNamed:@"DefaultImage"];
        }
        [mp3Array addObject:objMp3];
    }
}
}

-(UIImage *)getAlbumnArtWorkImage :(NSURL *)mp3Url{

AVAsset *asset = [AVURLAsset URLAssetWithURL:mp3Url options:nil];
UIImage *img = nil;
for (NSString *format in [asset availableMetadataFormats]) {
    for (AVMetadataItem *item in [asset metadataForFormat:format]) {
        if ([[item commonKey] isEqualToString:@"artwork"]) {
            img = [UIImage imageWithData:[item.value copyWithZone:nil]];
        }
    }
}
return img;
}

*** MP3ObjectsClass is a NSObject class. Using above function you can access device music files from iPhone.

Bista
  • 7,869
  • 3
  • 27
  • 55
Samrat Pramanik
  • 201
  • 3
  • 8
4

You can reference MPMediaPickerController class. It functions same as UIImagePickerController class.

Amresh Kumar
  • 1,445
  • 17
  • 30
  • OK, thank you! I'll check that right away..Just one question it works on all iOS 4.x and 5.x devices right? – Getsy Jan 30 '12 at 10:29
  • ios10 also needs NSAppleMusicUsageDescription in plist for the presentation of MPMediaPickerController to work. Otherwise it just flashes white and disappears. Same thing in cases when user has denied access to Music. Great job Apple! – Anton Tropashko Jun 15 '17 at 15:00
  • Is it legal to copy the iTunes purchased music into application sandbox and uploading to server ? – vishy Jun 22 '17 at 08:03
1

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;
    [self presentViewController:soundPicker animated:YES completion:nil];
}

-(void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
    MPMediaItem *item = [[mediaItemCollection items] objectAtIndex:0];
    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];
}

and play with [player play];

If you want to use AVAudioPlayer then import AudioToolbox/AudioToolbox.h

Bhumit Mehta
  • 16,278
  • 11
  • 50
  • 64
Vaibhav Sharma
  • 1,123
  • 10
  • 22
  • This is not help full to me can you explain in details so i can implement in my app. i want to add music library (all song) from my app & play. – Sandy Patel Feb 06 '15 at 12:33
  • NSURL *url will give you the exact url of selected audio File. and use MPMediaPickerControllerDelegate – Vaibhav Sharma Feb 23 '15 at 08:47