一、简述
对于影音娱乐类应用来说播放设备本地音乐是一项比较实用的功能,市面上的音乐播放器大多也都支持该功能。Apple提供两种方式来访问本地音乐库,分别是 MPMediaQuery 和 MPMediaPickerController。MPMediaPickerController 是一个已定制好UI的控件,为了满足自定义需求,通常我们使用 更强大的 MPMediaQuery 来获取本地音乐。
本文提供一个Demo简单展示如何使用 MPMediaQuery 获取本地音乐信息并使用 AVAudioPlayer 播放。如下图:
二、代码示例
#import "ViewController.h" #import <MediaPlayer/MediaPlayer.h> #import <AVFoundation/AVFoundation.h> @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,AVAudioPlayerDelegate> @property (nonatomic, strong) AVAudioPlayer *audioPlayer; @property (nonatomic, strong) MPMediaQuery *mediaQuery; @property (nonatomic, strong) UITableView *mediaTableView; @property (nonatomic, assign) NSInteger playingIndex; @end @implementation ViewController - (UITableView *)mediaTableView{ if (!_mediaTableView) { _mediaTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; _mediaTableView.delegate = self; _mediaTableView.dataSource = self; } return _mediaTableView; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.playingIndex = NSIntegerMax; self.mediaQuery = [MPMediaQuery songsQuery]; [self.view addSubview:self.mediaTableView]; UILabel *header = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds),64.0)]; header.text = @"iPod Library"; header.textColor = [UIColor brownColor]; header.textAlignment = NSTextAlignmentCenter; header.font = [UIFont systemFontOfSize:18.0]; [self.mediaTableView setTableHeaderView:header]; UIButton *footer = [UIButton buttonWithType:UIButtonTypeCustom]; footer.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 32.0); [footer setTitle:@"stop" forState:UIControlStateNormal]; footer.titleLabel.font = [UIFont systemFontOfSize:18.0]; [footer setTitleColor:[UIColor brownColor] forState:UIControlStateNormal]; [footer addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside]; [self.mediaTableView setTableFooterView:footer]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Action /** * 播放音频 * * @param itme <#itme description#> */ - (void)playMediaWithItem:(MPMediaItem *) itme{ self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:itme.assetURL error:nil]; self.audioPlayer.delegate = self; [self.audioPlayer prepareToPlay]; [self.audioPlayer play]; } /** * stop */ - (void)stop{ if (self.audioPlayer.playing) { [self.audioPlayer stop]; self.playingIndex = NSIntegerMax; [self.mediaTableView reloadData]; } } #pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.mediaQuery.items.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 66.0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellIdentifier = @"ipodMusicCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; } MPMediaItem *item = self.mediaQuery.items[indexPath.row]; cell.textLabel.text = item.title; cell.textLabel.textColor = [UIColor brownColor]; cell.detailTextLabel.text = item.artist; cell.detailTextLabel.textColor = [UIColor brownColor]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.tintColor = [UIColor brownColor]; if (indexPath.row == self.playingIndex) { cell.textLabel.textColor = [UIColor redColor]; cell.detailTextLabel.textColor = [UIColor redColor]; cell.accessoryType = UITableViewCellAccessoryCheckmark; cell.tintColor = [UIColor redColor]; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ MPMediaItem *item = self.mediaQuery.items[indexPath.row]; [self playMediaWithItem:item]; self.playingIndex = indexPath.row; [self.mediaTableView reloadData]; } #pragma mark - AVAudioPlayerDelegate - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ if (player == self.audioPlayer) { self.playingIndex ++; //自动下一曲 if (self.playingIndex < self.mediaQuery.items.count) { [self playMediaWithItem:self.mediaQuery.items[_playingIndex]]; } else { self.playingIndex = 0; [self playMediaWithItem:[self.mediaQuery.items firstObject]]; } [self.mediaTableView reloadData]; } } @end
时间: 2024-10-11 07:39:27