(只能访问音频文件,如music,podcast,audiobook等)
有两种播放器可以选择,一种是application music player,另外一种是iPod music player。
第一种播放器是一种内部播放器,当程序对出后停止播放;而第二种播放器则与iPod播放器内的信息相关,退出之后不会停止播放。获取方式如下:
播放之前需要设置播放器的播放队列
管理播放模式和播放状态的一些属性
播放状态 MPMusicPlaybackState
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
enum
{ MPMusicPlaybackStateStopped, MPMusicPlaybackStatePlaying, MPMusicPlaybackStatePaused, MPMusicPlaybackStateInterrupted, MPMusicPlaybackStateSeekingForward, MPMusicPlaybackStateSeekingBackward};typedef
NSInteger MPMusicPlaybackState; |
播放控制方法
播放状态发生变化时可以发送通知
MPMusicPlayerControllerPlaybackStateDidChangeNotification
可以通过该通知来改变播放按钮的样式
MPMusicPlayerControllerNowPlayingItemDidChangeNotification
MPMusicPlayerControllerVolumeDidChangeNotification
具体步骤
1.注册和开始发送通知
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
Listing
2-1 Registering for
and activating music player notificationsNSNotificationCenter
*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]; |
|
1
2
3
4
5
6
7
8
9
10
11
12
|
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]; |
|
1
2
3
4
5
6
|
Listing
2-3 Creating an application music playerMPMusicPlayerController*
appMusicPlayer = [MPMusicPlayerController
applicationMusicPlayer]; [appMusicPlayer
setShuffleMode: MPMusicShuffleModeOff];[appMusicPlayer
setRepeatMode: MPMusicRepeatModeNone]; |
|
1
2
3
4
5
6
7
8
|
Listing
2-4 Creating an iPod music playerMPMusicPlayerController*
iPodMusicPlayer = [MPMusicPlayerController
iPodMusicPlayer]; if
([iPodMusicPlayer nowPlayingItem]) { //
Update the UI (artwork, song name, volume indicator, etc.) //
to reflect the iPod state} |
|
1
2
3
4
5
6
7
8
9
10
|
-
(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];} |
主要是设置代理和选择多媒体类型,然后通过代理方法来获取选中的歌曲
|
1
2
3
4
5
6
7
8
9
10
11
12
|
#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];} |
|
1
|
-
(id) valueForProperty: (NSString *) property |
NSString *const MPMediaItemPropertyTitle;
NSString *const MPMediaItemPropertyAlbumTitle;
NSString *const MPMediaItemPropertyArtist;
|
1
|
-
(id) valueForProperty: (NSString *) property |
属性
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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); }} |
需要设置两个属性: filter and grouping type
filter描述查询内容,grouping type 描述返回内容的排列方式
查询可以获取items,也可以获取collections
|
1
2
3
4
5
6
7
|
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);} |
|
1
2
3
4
5
6
7
8
|
MPMediaPropertyPredicate
*artistNamePredicate = [MPMediaPropertyPredicate
predicateWithValue: @"Happy
the Clown" forProperty:
MPMediaItemPropertyArtist]; MPMediaQuery
*myArtistQuery = [[MPMediaQuery alloc] init];[myArtistQuery
addFilterPredicate: artistNamePredicate]; NSArray
*itemsFromArtistQuery = [myArtistQuery items]; |
|
1
2
3
4
5
6
7
8
9
10
11
12
|
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]; |
|
1
2
3
4
5
6
|
Listing
4-4 Applying multiple predicates when initializing a media queryNSSet
*predicates = [NSSet
setWithObjects: artistNamePredicate, albumNamePredicate, nil]; MPMediaQuery
*specificQuery = [[MPMediaQuery
alloc] initWithFilterPredicates: predicates]; |
|
1
2
3
4
5
6
7
|
Listing
4-5 Testing if
a property key can be used for
a media property predicateif
([MPMediaItem canFilterByProperty: MPMediaItemPropertyGenre]) { MPMediaPropertyPredicate
*rockPredicate = [MPMediaPropertyPredicate
predicateWithValue: @"Rock" forProperty:
MPMediaItemPropertyGenre]; [query
addFilterPredicate: rockPredicate];} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
Listing
4-6 Using grouping type to specify media item collectionsMPMediaQuery
*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); }} |

|
1
2
3
4
5
6
7
8
9
10
|
Listing
4-7 Displaying album artwork for
a media itemMPMediaItemArtwork
*artwork = [mediaItem
valueForProperty: MPMediaItemPropertyArtwork];UIImage
*artworkImage = [artwork
imageWithSize: albumImageView.bounds.size];if
(artworkImage) { albumImageView.image
= artworkImage;}
else
{ albumImageView.image
= [UIImage imageNamed: @"noArtwork.png"];} |
访问iPod Library及MPMusicPlayerController
原文:http://blog.csdn.net/u010079532/article/details/19248357