獲取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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM