IOS音樂庫的兩種方法application MusicPlayer iPodMusicPlayer


1.訪問音樂庫的兩種方法,如下圖

(只能訪問音頻文件,如music,podcast,audiobook等)

2.MPMusicPlayerController的使用

有兩種播放器可以選擇,一種是application music player,另外一種是iPod music player。

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


  • + 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


具體步驟

1.註冊和開始發送通知


  1. Listing 2-1 Registering for and activating music player notifications
  2. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  3. [notificationCenter
  4. addObserver: self
  5. selector: @selector (handle_NowPlayingItemChanged:)
  6. name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification
  7. object: musicPlayer];
  8. [notificationCenter
  9. addObserver: self
  10. selector: @selector (handle_PlaybackStateChanged:)
  11. name: MPMusicPlayerControllerPlaybackStateDidChangeNotification
  12. object: musicPlayer];
  13. [musicPlayer beginGeneratingPlaybackNotifications];



  1. Listing 2-2 Unregistering and deactivating music player notifications
  2. [[NSNotificationCenter defaultCenter]
  3. removeObserver: self
  4. name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification
  5. object: musicPlayer];
  6. [[NSNotificationCenter defaultCenter]
  7. removeObserver: self
  8. name: MPMusicPlayerControllerPlaybackStateDidChangeNotification
  9. object: musicPlayer];
  10. [musicPlayer endGeneratingPlaybackNotifications];


2.創建並配置一個Music Player


  1. Listing 2-3 Creating an application music player
  2. MPMusicPlayerController* appMusicPlayer =
  3. [MPMusicPlayerController applicationMusicPlayer];
  4. [appMusicPlayer setShuffleMode: MPMusicShuffleModeOff];
  5. [appMusicPlayer setRepeatMode: MPMusicRepeatModeNone];



  1. Listing 2-4 Creating an iPod music player
  2. MPMusicPlayerController* iPodMusicPlayer =
  3. [MPMusicPlayerController iPodMusicPlayer];
  4. if ([iPodMusicPlayer nowPlayingItem]) {
  5. // Update the UI (artwork, song name, volume indicator, etc.)
  6. // to reflect the iPod state
  7. }


3.設置播放隊列


  • – setQueueWithQuery:
  • – setQueueWithItemCollection:

4.控制播放




3.MPMediaPickerController的使用


  1. - (IBAction)addSongsToMusicPlayer:(id)sender
  2. {
  3. MPMediaPickerController *mpController = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
  4. mpController.delegate = self;
  5. mpController.prompt = @"Add songs to play";
  6. mpController.allowsPickingMultipleItems = YES;
  7. [self presentModalViewController:mpController animated:YES];
  8. [mpController release];
  9. }


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



  1. #pragma mark - Media Picker Delegate Methods
  2. - (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
  3. {
  4. [self.musicPlayer setQueueWithItemCollection:mediaItemCollection];
  5. [self dismissModalViewControllerAnimated:YES];
  6. }
  7. - (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
  8. {
  9. [self dismissModalViewControllerAnimated:YES];
  10. }


4.MPMediaItem

用此方法來獲取item的metadata
  1. - (id) valueForProperty: (NSString *) property


NSString *const MPMediaItemPropertyTitle;

NSString *const MPMediaItemPropertyAlbumTitle;

NSString *const MPMediaItemPropertyArtist;



5.MPMediaItemCollection

collection是一組有序的item集合,可用同樣的方法來獲取collection的metadata
  1. - (id) valueForProperty: (NSString *) property

創建
  • + collectionWithItems:
  • – initWithItems:

屬性

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

6.MPMediaPlaylist

  1. MPMediaQuery *myPlaylistsQuery = [MPMediaQuery playlistsQuery];
  2. NSArray *playlists = [myPlaylistsQuery collections];
  3. for (MPMediaPlaylist *playlist in playlists) {
  4. NSLog (@"%@", [playlist valueForProperty: MPMediaPlaylistPropertyName]);
  5. NSArray *songs = [playlist items];
  6. for (MPMediaItem *song in songs) {
  7. NSString *songTitle =
  8. [song valueForProperty: MPMediaItemPropertyTitle];
  9. NSLog (@"\t\t%@", songTitle);
  10. }
  11. }


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.


獲取全部歌曲
  1. MPMediaQuery *everything = [[MPMediaQuery alloc] init];
  2. NSLog(@"Logging items from a generic query...");
  3. NSArray *itemsFromGenericQuery = [everything items];
  4. for (MPMediaItem *song in itemsFromGenericQuery) {
  5. NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
  6. NSLog (@"%@", songTitle);
  7. }

獲取名爲“Happy the Clown”的藝術家的歌曲
  1. MPMediaPropertyPredicate *artistNamePredicate =
  2. [MPMediaPropertyPredicate predicateWithValue: @"Happy the Clown"
  3. forProperty: MPMediaItemPropertyArtist];
  4. MPMediaQuery *myArtistQuery = [[MPMediaQuery alloc] init];
  5. [myArtistQuery addFilterPredicate: artistNamePredicate];
  6. NSArray *itemsFromArtistQuery = [myArtistQuery items];

多個查找條件,查找名爲"Sad the Joker"的藝術家的"Stair Tumbling"專輯
  1. MPMediaPropertyPredicate *artistNamePredicate =
  2. [MPMediaPropertyPredicate predicateWithValue: @"Sad the Joker"
  3. forProperty: MPMediaItemPropertyArtist];
  4. MPMediaPropertyPredicate *albumNamePredicate =
  5. [MPMediaPropertyPredicate predicateWithValue: @"Stair Tumbling"
  6. forProperty: MPMediaItemPropertyAlbumTitle];
  7. MPMediaQuery *myComplexQuery = [[MPMediaQuery alloc] init];
  8. [myComplexQuery addFilterPredicate: artistNamePredicate];
  9. [myComplexQuery addFilterPredicate: albumNamePredicate];


  1. Listing 4-4 Applying multiple predicates when initializing a media query
  2. NSSet *predicates =
  3. [NSSet setWithObjects: artistNamePredicate, albumNamePredicate, nil];
  4. MPMediaQuery *specificQuery =
  5. [[MPMediaQuery alloc] initWithFilterPredicates: predicates];

  1. Listing 4-5 Testing if a property key can be used for a media property predicate
  2. if ([MPMediaItem canFilterByProperty: MPMediaItemPropertyGenre]) {
  3. MPMediaPropertyPredicate *rockPredicate =
  4. [MPMediaPropertyPredicate predicateWithValue: @"Rock"
  5. forProperty: MPMediaItemPropertyGenre];
  6. [query addFilterPredicate: rockPredicate];
  7. }


  1. Listing 4-6 Using grouping type to specify media item collections
  2. MPMediaQuery *query = [[MPMediaQuery alloc] init];
  3. [query addFilterPredicate: [MPMediaPropertyPredicate
  4. predicateWithValue: @"Moribund the Squirrel"
  5. forProperty: MPMediaItemPropertyArtist]];
  6. // Sets the grouping type for the media query
  7. [query setGroupingType: MPMediaGroupingAlbum];
  8. NSArray *albums = [query collections];
  9. for (MPMediaItemCollection *album in albums) {
  10. MPMediaItem *representativeItem = [album representativeItem];
  11. NSString *artistName =
  12. [representativeItem valueForProperty: MPMediaItemPropertyArtist];
  13. NSString *albumName =
  14. [representativeItem valueForProperty: MPMediaItemPropertyAlbumTitle];
  15. NSLog (@"%@ by %@", albumName, artistName);
  16. NSArray *songs = [album items];
  17. for (MPMediaItem *song in songs) {
  18. NSString *songTitle =
  19. [song valueForProperty: MPMediaItemPropertyTitle];
  20. NSLog (@"\t\t%@", songTitle);
  21. }
  22. }


query的一些簡便構造方法


專輯封面的使用

  1. Listing 4-7 Displaying album artwork for a media item
  2. MPMediaItemArtwork *artwork =
  3. [mediaItem valueForProperty: MPMediaItemPropertyArtwork];
  4. UIImage *artworkImage =
  5. [artwork imageWithSize: albumImageView.bounds.size];
  6. if (artworkImage) {
  7. albumImageView.image = artworkImage;
  8. } else {
  9. albumImageView.image = [UIImage imageNamed: @"noArtwork.png"];
  10. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章