Access device music files from iPhone app programmatically

最后都变了- 提交于 2019-11-29 02:37:07

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

Samrat Pramanik
-(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.

Vaibhav Sharma

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!