1. Summary
In many music player applications, you can play the music in your own music player. This player is called iPod. music files are stored in the iPod library.
This article describes how to play music in the iPod library through the music player that comes with the IOS system.
2. iOS development documentation: IPOD library access programming guide
3. Required framework: Media Player framework
4. basic functions of the framework:
1) obtain the playback queue of the iPod library through media query;
2) Select a playback queue through media picker controller;
3) use the music player controller to play music.
5. Technical implementation step by step
Step 1: Create an mpmusicplayercontroller player and select the player type: applicationmusicplayer or ipodmusicplayer. The difference between the two is that when ipodmusicplayer is used, the music can be played after the app is minimized, which means that the player that comes with the system can be played directly.
// Create a music player
Self. musicplayer = [mpmusicplayercontrolleripodmusicplayer];
Self. musicplayer. shufflemode
= Mpmusicshufflemodeoff;
Self. musicplayer. repeatmode
= Mpmusicrepeatmodenone;
Nsicationicationcenter * notificationcenter = [nsnotifcencenterdefacenter];
[Icationicationcenter
Addobserver: Self
Selector: @ selector (handlevolumechanged :)
Name: mpmusicplayercontrollervolumedidchangenotification
Object: Self. musicplayer];
[Icationicationcenter
Addobserver: Self
Selector: @ selector (handleplaybackstatechanged :)
Name: mpmusicplayercontrollerplaybackstatedidchangenotification
Object: Self. musicplayer];
[Icationicationcenter
Addobserver: Self
Selector: @ selector (handlenowplayingitemchanged :)
Name: mpmusicplayercontrollernowplayingitemdidchangenotification
Object: Self. musicplayer];
//
The following code can be added to implement notification to musicplayer.
[Self. musicplayerbegingeneratingplaybackications ications];
The code above contains some basic settings for musicplayer and adds the notification receiving function.
Step 2: Create a playback queue, that is, mediaquery, and assign it to musicplayer.
You can obtain a playback queue using either of the following methods:
1. directly obtain the playback queue in the iPod library and select music.
// Create a media Query
Mpmediaquery * mediaquery = [mpmediaqueryalbumsquery];
//
Add filter to select a specific queue
Mpmediapropertypredicate * artistnamepredicate = [mpmediapropertypredicatepredicatewithvalue: @ "July 22" forproperty: mpmediaitempropertyartist];
[Mediaqueryaddfilterpredicate: artistnamepredicate];
// Add a queue
Self. mediaqueue = [mediaqueryitems];
Self. usermediaitemcollection = [mpmediaitemcollectioncollectionwithitems: Self. mediaqueue];
[Self. musicplayersetqueuewithitemcollection: Self. usermediaitemcollection];
// Or
// [Self. musicplayer setqueuewithquery: mediaquery];
2. To use mpmediapickercontroller, add
Mpmediapickercontrollerdelegate
Mpmediapickercontroller * mediapickercontroller = [[mpmediapickercontrolleralloc] initwithmediatypes: mpmediatypemusic];
Mediapickercontroller. Delegate = self;
Mediapickercontroller. allowspickingmultipleitems = yes;
Mediapickercontroller. Prompt = @ "Add songs to play ";
Pass
-(Void) mediapicker :( mpmediapickercontroller *) mediapicker didpickmediaitems :( mpmediaitemcollection
*) Mediaitemcollection
Obtain the mediaitemcollection and add the musicplayer playing queue to the method.
# Describe the relationship between mediaitemcollection, mediaquery, and mediaitem:
Mediaitemcollection is similar to mediaquery, but mediaitemcollection can contain various groups. For example, mediaitemcollection is classified by album records, while mediaquery is not classified. Mediaitem is an item of both.
Mediaitemcollection and mediaquery can be converted to each other.
For example: [self. mediaquery setgrouptype: mpmediagroupingalbum];
Nsarray * albums = [self. mediaquery collections];
In this case, albums contains the object of mpmediaitemcollection.
Step 3: Set and operate musicplayer
The basic methods and properties are play, pause, stop, skiptonextitem, volume, currentplaybacktime, repeat, and shuffle. These are all very simple. Focus on notification
Step 4: Obtain music information
You can use [mediaitem valueforproperty:...] to obtain various mediaitem information.
For example
Self. songname. Title = [mediaitemvalueforproperty: mpmediaitempropertytitle];
The above is the basic implementation method.