iOS Get music library information

Source: Internet
Author: User

1two ways to access the music library, such as (access only audio files, such as Music,podcast,audiobook, etc.)2. Mpmusicplayercontroller is available in two different players, one for application music player and the other for ipod music player. The first player is an internal player that stops playing when the program is out of the way, while the second player is related to the information in the ipod player and does not stop playing after exiting. Here's how to get it:+Applicationmusicplayer+Ipodmusicplayer playback queue needs to be set before playing –setqueuewithquery:–setqueuewithitemcollection: Some properties for managing playback mode and playback status Currentplaybacktime Property Nowplayingitem Property Playbackstate Property Repeatmode Property Shufflemode Prope Rty Volume property playback status Mpmusicplaybackstateenum{mpmusicplaybackstatestopped, mpmusicplaybackstateplaying, mpmusicplaybackstatepaused, MPMusicPlaybackStateI nterrupted, Mpmusicplaybackstateseekingforward, Mpmusicplaybackstateseekingbackward};typedef NSInteger Mpmusicplaybackstate; Playback control Method –play–pause–stop–beginseekingforward–beginseekingbackward–endseeking–skiptonextitem –skiptobeginning–skiptopreviousitem You can send notifications when the playback status changes –begingeneratingplaybacknotifications– Endgeneratingplaybacknotificationsmpmusicplayercontrollerplaybackstatedidchangenotification can change the style of the play button by this notification Mpmusicplayer Controllernowplayingitemdidchangenotificationmpmusicplayercontrollervolumedidchangenotification Concrete Steps1. Sign up and start sending notifications [plain] view Plaincopyprint?Listing2-1Registering forand activating music player Notifications Nsnotificationcenter*notificationcenter =[Nsnotificationcenter Defaultcenter]; [Notificationcenter addobserver:self selector: @selector (handle_nowplayingitemchanged:) name:m PmusicplayercontrollernowplayingitemdidchangenotificationObject: Musicplayer]; [Notificationcenter addobserver:self selector: @selector (handle_playbackstatechanged:) name:mp MusicplayercontrollerplaybackstatedidchangenotificationObject: Musicplayer];    [Musicplayer begingeneratingplaybacknotifications]; [Plain] View Plaincopyprint?Listing2-2Unregistering and deactivating music player notifications [[Nsnotificationcenter Defaultcenter] Removeobserver : Self name:mpmusicplayercontrollernowplayingitemdidchangenotificationObject: Musicplayer]; [[Nsnotificationcenter Defaultcenter] removeobserver:self name:mpmusicplayercontrollerplaybackstated IdchangenotificationObject: Musicplayer];  [Musicplayer endgeneratingplaybacknotifications]; 2. Create and configure a music Player [plain] View Plaincopyprint?Listing2-3Creating An application music player Mpmusicplayercontroller* Appmusicplayer =[Mpmusicplayercontroller Applicationmusicplayer];  [Appmusicplayer Setshufflemode:mpmusicshufflemodeoff];    [Appmusicplayer Setrepeatmode:mpmusicrepeatmodenone]; [Plain] View Plaincopyprint?Listing2-4Creating 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. Set the playback queue –setqueuewithquery:–setqueuewithitemcollection:4. Controlling Playback3. Use of Mpmediapickercontroller [plain] View Plaincopyprint? <span >-(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>The main is to set up the agent and select the multimedia type, and then through the proxy method to get the selected songs [plain] view Plaincopyprint? <span >#pragmaMark-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]; }</span>4. Mpmediaitem Use this method to get the item's Metadata[plain] View Plaincopyprint? - (ID) Valueforproperty: (NSString *) Property NSString*ConstMpmediaitempropertytitle; NSString*ConstMpmediaitempropertyalbumtitle; NSString*Constmpmediaitempropertyartist; 5. Mpmediaitemcollectioncollection is a set of ordered item collections that can be used in the same way to get collection Metadata[plain] View Plaincopyprint? - (ID) Valueforproperty: (NSString *) Property Creation+Collectionwithitems:–initwithitems: Properties of the Items property Representativeitem attribute Count property MediaTypes p Roperty6. Mpmediaplaylist[plain] View Plaincopyprint?Mpmediaquery*myplaylistsquery =[Mpmediaquery Playlistsquery]; Nsarray*playlists =[Myplaylistsquery collections];  for(Mpmediaplaylist *playlistinchplaylists) {NSLog (@"%@", [playlist valueforproperty:mpmediaplaylistpropertyname]); Nsarray*songs =[playlist items];  for(Mpmediaitem *songinchsongs) {NSString*songtitle =[Song Valueforproperty:mpmediaitempropertytitle]; NSLog (@"\t\t%@", SongTitle); }  }   7. Mpmediaquery needs to set two properties: Filter and Grouping TypeFilter describes the contents of the query, grouping type describes the arrangement of the returned content query can get items, you can also get Collectionswhen You ask forItems, the query returns a collection containing all the items of that match the filter. The items areinch"Natural" order, meaning that they is ordered asITunes shows them on the desktop. when do you ask forcollections, the media query employs not only it filter but also its grouping type. Get all songs [plain] View Plaincopyprin T?Mpmediaquery*everything =[[Mpmediaquery alloc] init]; NSLog (@"Logging items from a generic query ..."); Nsarray*itemsfromgenericquery =[everything items];  for(Mpmediaitem *songinchitemsfromgenericquery) {NSString*songtitle =[Song Valueforproperty:mpmediaitempropertytitle]; NSLog (@"%@", SongTitle); Get the song of the artist named "Happy the Clown" [plain] View Plaincopyprint?mpmediapropertypredicate*artistnamepredicate =[mpmediapropertypredicate predicatewithvalue:@"Happy the Clown"Forproperty:mpmediaitempropertyartist]; Mpmediaquery*myartistquery =[[Mpmediaquery alloc] init];     [Myartistquery Addfilterpredicate:artistnamepredicate]; Nsarray*itemsfromartistquery =[myartistquery items]; Multiple find criteria, looking for the name"Sad the Joker"The artist's"Stair Tumbling"album [plain] View Plaincopyprint?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]; [Plain] View Plaincopyprint?Listing4-4applying multiple predicates when initializing a media query Nsset*predicates =[Nsset setwithobjects:artistnamepredicate, albumnamepredicate, nil]; Mpmediaquery*specificquery =[[Mpmediaquery alloc] initwithfilterpredicates:predicates]; [Plain] View Plaincopyprint?Listing4-5TestingifA property key can be used fora media property predicateif([Mpmediaitem canfilterbyproperty:mpmediaitempropertygenre]) {mpmediapropertypredicate*rockpredicate =[mpmediapropertypredicate predicatewithvalue:@"Rock"Forproperty:mpmediaitempropertygenre];  [Query addfilterpredicate:rockpredicate]; } [Plain] view Plaincopyprint?Listing4-6Using 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 *albuminchalbums) {Mpmediaitem*representativeitem =[album Representativeitem]; NSString*artistname =[Representativeitem valueforproperty:mpmediaitempropertyartist]; NSString*albumname =[Representativeitem Valueforproperty:mpmediaitempropertyalbumtitle]; NSLog (@"%@ by%@", Albumname, artistname); Nsarray*songs =[album items];  for(Mpmediaitem *songinchsongs) {NSString*songtitle =[Song Valueforproperty:mpmediaitempropertytitle]; NSLog (@"\t\t%@", SongTitle); }} Some simple ways to construct an album cover using [plain] view Plaincopyprint?Listing4-7Displaying album artwork fora 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"]; }  

iOS Get Music library information (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.