2017-04-21 54 views
-2

我可以使用以下代碼訪問我從iTunes購買的音樂專輯。如何以編程方式從iOS訪問本地音樂文件?

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

但無法列出從我的電腦手動添加的音樂專輯。 iTunes最近能夠顯示它們。但我的應用程序不顯示它們?

如何進入手動添加的音樂文件?

+0

請評論你們爲什麼投票, –

回答

0

請嘗試以下操作:

首先導入AVFoundation/AVFoundation.h框架。

#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]; 
} 

您可以

[player play] 

注玩法:如果你想一次加載所有數據請看看下面的鏈接。這可能會立即幫助你。

Fetch All Audio & Video files in ios

相關問題