ios獲取本地音樂庫音樂

1.MPMediaItem 轉換成 NSUrl

NSURL* assetUrl = [mediaItem valueForProperty:MPMediaItemPropertyAssetURL];

2.MPMusicPlayerController的使用

有兩種播放器能夠選擇,一種是application music player,另一種是iPod music player。app

第一種播放器是一種內部播放器,當程序對出後中止播放;而第二種播放器則與iPod播放器內的信息相關,退出以後不會中止播放。獲取方式以下:ui

+ applicationMusicPlayer
+ iPodMusicPlayer


播放以前須要設置播放器的播放隊列

– setQueueWithQuery:
– setQueueWithItemCollection:
管理播放模式和播放狀態的一些屬性

  currentPlaybackTime  property
  nowPlayingItem  property
  playbackState  property
  repeatMode  property
  shuffleMode  property
  volume  property
播放狀態 MPMusicPlaybackState

enum {

   MPMusicPlaybackStateStopped,

   MPMusicPlaybackStatePlaying,

   MPMusicPlaybackStatePaused,

   MPMusicPlaybackStateInterrupted,

   MPMusicPlaybackStateSeekingForward,

   MPMusicPlaybackStateSeekingBackward

};

typedef NSInteger MPMusicPlaybackState;



播放控制方法

– play
– pause
– stop
– beginSeekingForward
– beginSeekingBackward
– endSeeking
– skipToNextItem
– skipToBeginning
– skipToPreviousItem
播放狀態發生變化時能夠發送通知

– beginGeneratingPlaybackNotifications
– endGeneratingPlaybackNotifications
MPMusicPlayerControllerPlaybackStateDidChangeNotification

能夠經過該通知來改變播放按鈕的樣式

MPMusicPlayerControllerNowPlayingItemDidChangeNotification

MPMusicPlayerControllerVolumeDidChangeNotification

具體步驟spa

1.註冊和開始發送通知代理

  1. Listing 2-1  Registering for and activating music player notifications  
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];  
       
    [notificationCenter  
        addObserver: self  
        selector:    @selector (handle_NowPlayingItemChanged:)  
        name:        MPMusicPlayerControllerNowPlayingItemDidChangeNotification  
        object:      musicPlayer];  
       
    [notificationCenter  
        addObserver: self  
        selector:    @selector (handle_PlaybackStateChanged:)  
        name:        MPMusicPlayerControllerPlaybackStateDidChangeNotification  
        object:      musicPlayer];  
       
    [musicPlayer beginGeneratingPlaybackNotifications];  
    
    
    Listing 2-2  Unregistering and deactivating music player notifications  
    [[NSNotificationCenter defaultCenter]  
        removeObserver: self  
        name:           MPMusicPlayerControllerNowPlayingItemDidChangeNotification  
        object:         musicPlayer];  
       
    [[NSNotificationCenter defaultCenter]  
        removeObserver: self  
        name:           MPMusicPlayerControllerPlaybackStateDidChangeNotification  
        object:         musicPlayer];  
       
    [musicPlayer endGeneratingPlaybackNotifications];  

    2.建立並配置一個Music Player
  1. Listing 2-3  Creating an application music player  
    MPMusicPlayerController* appMusicPlayer =  
        [MPMusicPlayerController applicationMusicPlayer];  
       
    [appMusicPlayer setShuffleMode: MPMusicShuffleModeOff];  
    [appMusicPlayer setRepeatMode: MPMusicRepeatModeNone];  
    
    
    Listing 2-4  Creating an iPod music player  
    
    MPMusicPlayerController* iPodMusicPlayer =  
        [MPMusicPlayerController iPodMusicPlayer];  
       
    if ([iPodMusicPlayer nowPlayingItem]) {  
        // Update the UI (artwork, song name, volume indicator, etc.)  
        //        to reflect the iPod state  
    }  

     


3.設置播放隊列code

  • – setQueueWithQuery:
  • – setQueueWithItemCollection:

4.控制播放

 server

3.MPMediaPickerController的使用

- (IBAction)addSongsToMusicPlayer:(id)sender  
{  
    MPMediaPickerController *mpController = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];  
    mpController.delegate = self;  
    mpController.prompt = @"Add songs to play";  
    mpController.allowsPickingMultipleItems = YES;  
      
    [self presentModalViewController:mpController animated:YES];  
    [mpController release];  
}  
</span>  


主要是設置代理和選擇多媒體類型,而後經過代理方法來獲取選中的歌曲隊列

 

  1. #pragma mark - Media Picker Delegate Methods  
      
    - (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection  
    {  
        [self.musicPlayer setQueueWithItemCollection:mediaItemCollection];  
        [self dismissModalViewControllerAnimated:YES];  
    }  
      
    - (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker  
    {  
        [self dismissModalViewControllerAnimated:YES];  
    }

     

4.MPMediaItemip

用此方法來獲取item的metadataci

 

  1. - (id) valueForProperty: (NSString *) property  

 

NSString *const MPMediaItemPropertyTitle;                   rem

NSString *const MPMediaItemPropertyAlbumTitle;              

NSString *const MPMediaItemPropertyArtist;                 

 

 

5.MPMediaItemCollection

collection是一組有序的item集合,可用一樣的方法來獲取collection的metadata

- (id) valueForProperty: (NSString *) property  

 

建立

  • + collectionWithItems:
  • – initWithItems:

屬性

  •   items  property
  •   representativeItem  property
  •   count  property
  •   mediaTypes  property

6.MPMediaPlaylist

  1. MPMediaQuery *myPlaylistsQuery = [MPMediaQuery playlistsQuery];  
    NSArray *playlists = [myPlaylistsQuery collections];  
       
    for (MPMediaPlaylist *playlist in playlists) {  
        NSLog (@"%@", [playlist valueForProperty: MPMediaPlaylistPropertyName]);  
       
        NSArray *songs = [playlist items];  
        for (MPMediaItem *song in songs) {  
            NSString *songTitle =  
                [song valueForProperty: MPMediaItemPropertyTitle];  
            NSLog (@"\t\t%@", songTitle);  
        }  
    }  

     

7.MPMediaQuery

須要設置兩個屬性: filter  and  grouping type

filter描述查詢內容,grouping type 描述返回內容的排列方式

 

 

查詢能夠獲取items,也能夠獲取collections

  • When you ask for items, the query returns a collection containing all the items that match the filter. The items are in 「natural」 order, meaning that they are ordered as iTunes shows them on the desktop.
  • When you ask for collections, the media query employs not only its filter but also its grouping type.

 

獲取所有歌曲

MPMediaQuery *everything = [[MPMediaQuery alloc] init];   

NSLog(@"Logging items from a generic query...");  
NSArray *itemsFromGenericQuery = [everything items];  
for (MPMediaItem *song in itemsFromGenericQuery) {  
    NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];  
    NSLog (@"%@", songTitle);  
}  

 

獲取名爲「Happy the Clown」的藝術家的歌曲

 

  1. MPMediaPropertyPredicate *artistNamePredicate =  
        [MPMediaPropertyPredicate predicateWithValue: @"Happy the Clown"  
                                         forProperty: MPMediaItemPropertyArtist];  
       
    MPMediaQuery *myArtistQuery = [[MPMediaQuery alloc] init];  
    [myArtistQuery addFilterPredicate: artistNamePredicate];  
       
    NSArray *itemsFromArtistQuery = [myArtistQuery items];  

     


多個查找條件,查找名爲"Sad the Joker"的藝術家的"Stair Tumbling"專輯

  1. MPMediaPropertyPredicate *artistNamePredicate =  
        [MPMediaPropertyPredicate predicateWithValue: @"Sad the Joker"  
                                         forProperty: MPMediaItemPropertyArtist];  
       
    MPMediaPropertyPredicate *albumNamePredicate =  
        [MPMediaPropertyPredicate predicateWithValue: @"Stair Tumbling"  
                                         forProperty: MPMediaItemPropertyAlbumTitle];  
       
    MPMediaQuery *myComplexQuery = [[MPMediaQuery alloc] init];  
       
    [myComplexQuery addFilterPredicate: artistNamePredicate];  
    [myComplexQuery addFilterPredicate: albumNamePredicate];  
    
    
    Listing 4-4  Applying multiple predicates when initializing a media query  
    NSSet *predicates =  
        [NSSet setWithObjects: artistNamePredicate, albumNamePredicate, nil];  
       
    MPMediaQuery *specificQuery =  
        [[MPMediaQuery alloc] initWithFilterPredicates: predicates];  
    
    Listing 4-5  Testing if a property key can be used for a media property predicate  
    
    if ([MPMediaItem canFilterByProperty: MPMediaItemPropertyGenre]) {  
        MPMediaPropertyPredicate *rockPredicate =  
            [MPMediaPropertyPredicate predicateWithValue: @"Rock"  
                                             forProperty: MPMediaItemPropertyGenre];  
        [query addFilterPredicate: rockPredicate];  
    }  
    
    
    Listing 4-6  Using grouping type to specify media item collections  
    MPMediaQuery *query = [[MPMediaQuery alloc] init];  
       
    [query addFilterPredicate: [MPMediaPropertyPredicate  
                                   predicateWithValue: @"Moribund the Squirrel"  
                                          forProperty: MPMediaItemPropertyArtist]];  
    // Sets the grouping type for the media query  
    [query setGroupingType: MPMediaGroupingAlbum];  
       
    NSArray *albums = [query collections];  
    for (MPMediaItemCollection *album in albums) {  
        MPMediaItem *representativeItem = [album representativeItem];  
        NSString *artistName =  
            [representativeItem valueForProperty: MPMediaItemPropertyArtist];  
        NSString *albumName =  
            [representativeItem valueForProperty: MPMediaItemPropertyAlbumTitle];  
        NSLog (@"%@ by %@", albumName, artistName);  
       
        NSArray *songs = [album items];  
        for (MPMediaItem *song in songs) {  
            NSString *songTitle =  
                [song valueForProperty: MPMediaItemPropertyTitle];  
            NSLog (@"\t\t%@", songTitle);  
        }  
    }  

     

 

 

query的一些簡便構造方法



專輯封面的使用


 

Listing 4-7  Displaying album artwork for a media item  

MPMediaItemArtwork *artwork =  
    [mediaItem valueForProperty: MPMediaItemPropertyArtwork];  
UIImage *artworkImage =  
    [artwork imageWithSize: albumImageView.bounds.size];  
if (artworkImage) {  
    albumImageView.image = artworkImage;  
} else {  
    albumImageView.image = [UIImage imageNamed: @"noArtwork.png"];  
}  
相關文章
相關標籤/搜索