ios获取音乐库信息(转)

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.注册和开始发送通知

[plain] view plaincopyprint? 

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];  

[plain] view plaincopyprint? 

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

[plain] view plaincopyprint? 

Listing 2-3  Creating an application music player
MPMusicPlayerController* appMusicPlayer =
    [MPMusicPlayerController applicationMusicPlayer];  

[appMusicPlayer setShuffleMode: MPMusicShuffleModeOff];
[appMusicPlayer setRepeatMode: MPMusicRepeatModeNone];  

[plain] view plaincopyprint? 

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.设置播放队列

– setQueueWithQuery:
– setQueueWithItemCollection:
4.控制播放

3.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>  

主要是设置代理和选择多媒体类型,然后通过代理方法来获取选中的歌曲

[plain] view plaincopyprint? 

<span >#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];
}</span>  

4.MPMediaItem
用此方法来获取item的metadata
[plain] view plaincopyprint? 

- (id) valueForProperty: (NSString *) property  

NSString *const MPMediaItemPropertyTitle;
NSString *const MPMediaItemPropertyAlbumTitle;
NSString *const MPMediaItemPropertyArtist;                 

5.MPMediaItemCollection
collection是一组有序的item集合,可用同样的方法来获取collection的metadata
[plain] view plaincopyprint? 

- (id) valueForProperty: (NSString *) property  

创建
+ collectionWithItems:
– initWithItems:
属性
  items  property
  representativeItem  property
  count  property
  mediaTypes  property
6.MPMediaPlaylist

[plain] view plaincopyprint? 

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.

获取全部歌曲
[plain] view plaincopyprint? 

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”的艺术家的歌曲
[plain] view plaincopyprint? 

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"专辑
[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? 

Listing 4-4  Applying multiple predicates when initializing a media query
NSSet *predicates =
    [NSSet setWithObjects: artistNamePredicate, albumNamePredicate, nil];  

MPMediaQuery *specificQuery =
    [[MPMediaQuery alloc] initWithFilterPredicates: predicates];  

[plain] view plaincopyprint? 

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];
}  

[plain] view plaincopyprint? 

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的一些简便构造方法

专辑封面的使用

[plain] view plaincopyprint? 

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"];
}  
时间: 2024-10-10 16:21:16

ios获取音乐库信息(转)的相关文章

ios获取音乐库信息

1.访问音乐库的两种方法,如下图 (只能访问音频文件,如music,podcast,audiobook等) 2.MPMusicPlayerController的使用 有两种播放器可以选择,一种是application music player,另外一种是iPod music player. 第一种播放器是一种内部播放器,当程序对出后停止播放:而第二种播放器则与iPod播放器内的信息相关,退出之后不会停止播放.获取方式如下: + applicationMusicPlayer + iPodMusic

iOS获取应用程序信息,版本号,程序名等

转载▼     iOS获取应用程序信息 NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; 其中的信息示范: 版本号:[infoDictionary objectForKey:@"CFBundleVersion"]; 应用程序名:[infoDictionary objectForKey:@"CFBundleDisplayName"]; { CFBundleDevelopment

iOS获取手机相关信息

iOS具体的设备型号: #include <sys/types.h> #include <sys/sysctl.h> - (void)test { //手机型号. size_t size; sysctlbyname("hw.machine", NULL, &size, NULL, 0); char *machine = (char*)malloc(size); sysctlbyname("hw.machine", machine, &

iOS获取设备版本信息

系统提供的接口[[UIDevice currentDevice] model]只能获取iphone,ipad无法具体到iphone6等, 下面这个接口可以获取到具体的,后续有新的系统在增加 + (NSString*)deviceVersion { // 需要加入#import "sys/utsname.h" struct utsname systemInfo; uname(&systemInfo); NSString *deviceString = [NSString stri

iOS 获取APP相关信息 私有API

/* Generated by RuntimeBrowser Image: /System/Library/Frameworks/MobileCoreServices.framework/MobileCoreServices */ @interface LSApplicationWorkspace : NSObject // Image: /System/Library/Frameworks/MobileCoreServices.framework/MobileCoreServices + (i

iOS获取当前路由信息

导入头文件: #import <SystemConfiguration/CaptiveNetwork.h> - (void)currentWifiSSID {    // Does not work on the simulator.    NSArray *ifs = (__bridge id)CNCopySupportedInterfaces();    NSLog(@"ifs:%@",ifs);    for (NSString *ifnam in ifs) {   

iOS 获取键盘相关信息

一,在需要的地方添加监听 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeybo

ios 获取通讯录的所有信息

iOS获取通讯录全部信息 ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook); for(int i = 0; i < CFArrayGetCount(results); i++) { ABRecordRef person = CFArrayGetValueAtIndex(results, i); //读取f

Use GraceNote SDK in iOS(二)获取音乐的完整信息

在需求彻底明朗化,外加从MusicFans转到GraceNote,再从GraceNote的GNSDK转到iOS SDK后,终于完成了在iOS上通过音乐的部分信息获取完整信息的功能了.(好吧,我承认是相对完整...) 首先介绍下在项目中配置GraceNote的iOS SDK. SDK的下载地址:Mobile Client 注意要先登录才能见到文件的下载链接.另外官网还给出来一个SDK的配置文档,完全跟着走在Xcode 5是走不通的,不过也具有一定的指导作用,建议看一看. 下载解压后,新建一个工程,