获取ios本地乐库音乐MPMediaPickerController
一.通过弹出控制器,获取本地音乐列表,然后播放。该方法类似于UIImagePickerController。
- 导入MediaPlayer.framework框架。
1 -(void)musicInit{ 2 //创建播放器控制器 3 MPMediaPickerController *mpc = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio]; 4 //设置代理 5 mpc.delegate = self; 6 [self presentViewController:mpc animated:YES completion:nil]; 7 }
- 实现代理<MPMediaPickerControllerDelegate>,及其两个方法:
1 #pragma mark - Delegate 2 //选中后调用 3 - (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection{ 4 NSArray *items = mediaItemCollection.items; 5 MPMediaItem *item = [items objectAtIndex:0]; 6 NSString *name = [item valueForProperty:MPMediaItemPropertyTitle]; 7 NSLog(@"name= %@",name); 8 MPMediaItemArtwork *artwork = [item valueForProperty:MPMediaItemPropertyArtwork]; 9 UIImage *image = [artwork imageWithSize:CGSizeMake(100, 100)];//获取图片 10 // MPMediaItemPropertyPlaybackDuration 总时间的属性名称 11 12 // MPMusicPlayerController *mpc = [MPMusicPlayerController iPodMusicPlayer]; //调用ipod播放器 13 MPMusicPlayerController *mpc = [MPMusicPlayerController applicationMusicPlayer]; 14 //设置播放集合 15 [mpc setQueueWithItemCollection:mediaItemCollection]; 16 [mpc play]; 17 18 [self dismissViewControllerAnimated:YES completion:nil]; 19 } 20 //点击取消时回调 21 - (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker{ 22 [self dismissViewControllerAnimated:YES completion:nil]; 23 }
二. 仅获取本地音乐的列表
- 导入MediaPlayer.framework框架。
1 #import <UIKit/UIKit.h> 2 #import <MediaPlayer/MediaPlayer.h> 3 4 @interface MusicListTableViewController : UITableViewController 5 6 @property (nonatomic,retain) NSMutableArray *items; //存放本地歌曲 7 @property (nonatomic,retain) MPMusicPlayerController *mpc; //播放器对象 8 9 @end
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 5 self.items = [NSMutableArray array]; 6 //监听歌曲播放完成的通知 7 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reload) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:nil]; 8 [self initMusicItems]; 9 } 10 11 - (void)didReceiveMemoryWarning 12 { 13 [super didReceiveMemoryWarning]; 14 // Dispose of any resources that can be recreated. 15 } 16 17 #pragma mark - Private Method 18 -(void)initMusicItems{ 19 //获得query,用于请求本地歌曲集合 20 MPMediaQuery *query = [MPMediaQuery songsQuery]; 21 //循环获取得到query获得的集合 22 for (MPMediaItemCollection *conllection in query.collections) { 23 //MPMediaItem为歌曲项,包含歌曲信息 24 for (MPMediaItem *item in conllection.items) { 25 [self.items addObject:item]; 26 } 27 } 28 //通过歌曲items数组创建一个collection 29 MPMediaItemCollection *mic = [[MPMediaItemCollection alloc] initWithItems:self.items]; 30 //获得应用播放器 31 self.mpc = [MPMusicPlayerController applicationMusicPlayer]; 32 //开启播放通知,不开启,不会发送歌曲完成,音量改变的通知 33 [self.mpc beginGeneratingPlaybackNotifications]; 34 //设置播放的集合 35 [self.mpc setQueueWithItemCollection:mic]; 36 } 37 38 -(void)reload{ 39 //音乐播放完成刷新table 40 [self.tableView reloadData]; 41 } 42 43 #pragma mark - Table view data source 44 45 46 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 47 { 48 return self.items.count; 49 } 50 51 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 52 { 53 static NSString *CellIdentifier = @"MusicCellIdentifier"; 54 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 55 56 57 MPMediaItem *item = self.items[indexPath.row]; 58 //获得专辑对象 59 MPMediaItemArtwork *artwork = [item valueForProperty:MPMediaItemPropertyArtwork]; 60 //专辑封面 61 UIImage *img = [artwork imageWithSize:CGSizeMake(100, 100)]; 62 if (!img) { 63 img = [UIImage imageNamed:@"musicImage.png"]; 64 } 65 cell.imageView.image = img; 66 cell.textLabel.text = [item valueForProperty:MPMediaItemPropertyTitle]; //歌曲名称 67 cell.detailTextLabel.text = [item valueForProperty:MPMediaItemPropertyArtist]; //歌手名称 68 if (self.mpc.nowPlayingItem == self.items[indexPath.row]) { 69 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 70 }else{ 71 cell.accessoryType = UITableViewCellAccessoryNone; 72 } 73 74 75 return cell; 76 } 77 78 79 80 #pragma mark - Table view delegate 81 82 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 83 { 84 [self.tableView deselectRowAtIndexPath:indexPath animated:NO]; 85 //设置播放选中的歌曲 86 [self.mpc setNowPlayingItem:self.items[indexPath.row]]; 87 [self.mpc play]; 88 89 [self.tableView reloadData]; 90 }