前段时间因为从手机上传的音乐都是.aac格式的,以前也不知道还有这种格式的音乐,还好mac上有自带的播放器来播放音乐,但是那个播放器确实不是很好用,每个aac文件都需要打开一个播放器,然后手动播放。确实很不好用,抱怨之余,就想着自己来动手编写一个播放器。仅供自己娱乐,肯定会有很多编程不当的地方,也给自己以后写代码一个参考吧。话不多说,开始动手了。
首先新建singleview工程,在viewcontroller里添加相应的按钮(播放,下一首,上一首,单曲循环),tableview用于显示歌曲列表,label用于显示当前的歌曲。界面如下图:
在ViewController的类里添加tableview的两个代理<UITableViewDataSource,UITableViewDelegate>,然后在viewdidload里设置代理对象为self,注意一定要在viewdidload里设置。代码如下:
self.musicListTable.delegate = self;
self.musicListTable.dataSource = self;
{
AVAudioPlayer *_myMusicPlayer;
NSInteger _currentMusicCount; // 当前曲目位置
NSInteger _allSongsNum; // 所有曲目数
}
@property (strong, nonatomic) NSMutableArray *allSongsPathArray;
@property (strong, nonatomic) IBOutlet UIButton *playMusicButton;
@property (strong, nonatomic) IBOutlet UILabel *MusicInfoShow;
@property (strong, nonatomic) IBOutlet UITableView *musicListTable;
@property (strong, nonatomic) NSMutableArray *musicNameArray;
@property (strong, nonatomic) IBOutlet UISlider *progressSlider;
接着在ViewController的init方法里写入:
-(id)init
{
self = [super init];
if (self) {
self.allSongsPathArray = [[NSMutableArray alloc] initWithCapacity:0];
self.musicNameArray = [[NSMutableArray alloc] initWithCapacity:0];
_currentMusicCount = 0;
}
return self;
}
-(void)initMusic
{
NSString *path = MUSIC_PATH; // MUSIC_PATH为音乐文件所在的路径
NSArray *fileList = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]
pathsMatchingExtensions:[NSArray arrayWithObjects:@"aac",@"mp3", nil]];
[fileList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *musicPath = [NSString stringWithFormat:@"%@/%@",path,(NSString *)obj];
// [musicPath componentsSeparatedByString:@"/"];
[self.musicNameArray addObject:obj]; // 设置数组存储所有的歌曲名
[self.allSongsPathArray addObject:musicPath]; // 设置数组存储所有的歌曲所在的路径
}];
_allSongsNum = self.musicNameArray.count;
}
-(void)loadMusic:(NSString*)musicPath
{
if ([[NSFileManager defaultManager] fileExistsAtPath:musicPath])
{
NSURL *musicURL = [NSURL fileURLWithPath:musicPath];
NSError *myError = nil;
// 创建播放器
_myMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:&myError];
_myMusicPlayer.delegate = self;
if (_myMusicPlayer == nil)
{
NSLog(@"error %@",[myError description]);
}
[_myMusicPlayer setVolume:1];
_myMusicPlayer.numberOfLoops = 0;
[_myMusicPlayer prepareToPlay];
}
}
- (IBAction)play:(id)sender
{
if(_myMusicPlayer.playing)
{
[sender setTitle:@"Pause"];
[_myMusicPlayer pause];
}
else
{
[sender setTitle:@"Play"];
[_myMusicPlayer play];
}
}
- (IBAction)nextOne:(id)sender
{
if (_currentMusicCount < _allSongsNum - 2) {
_currentMusicCount++;
}
else
_currentMusicCount = 0;
[self musicPlayWithInfoShow];
}
- (IBAction)preforeOne:(id)sender
{
if(_currentMusicCount > 0)
_currentMusicCount--;
else
_currentMusicCount = _allSongsNum - 1;
[self musicPlayWithInfoShow];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
self.musicListTable.delegate = self;
self.musicListTable.dataSource = self;
self.progressSlider.minimumValue = 0;
self.progressSlider.maximumValue = 4.5;
[self.musicListTable reloadData];
[self initMusic];
self.MusicInfoShow.text = [self.musicNameArray objectAtIndex:_currentMusicCount];
[self loadMusic:[self.allSongsPathArray objectAtIndex:_currentMusicCount]];
[_myMusicPlayer play];
}
单曲循环对应的事件
- (IBAction)singleCircleSwitch:(id)sender
{
if ([sender isOn])
{
_myMusicPlayer.numberOfLoops = -1;
}
else
{
_myMusicPlayer.numberOfLoops = 0;
}
}
-(void)musicPlayWithInfoShow
{
[self loadMusic:[self.allSongsPathArray objectAtIndex:_currentMusicCount]];
self.MusicInfoShow.text = [self.musicNameArray objectAtIndex:_currentMusicCount];
NSIndexPath *currentIndexPath = [NSIndexPath indexPathForRow:_currentMusicCount inSection:0];
[self.musicListTable selectRowAtIndexPath:currentIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
[_myMusicPlayer play];
}
#pragma mark-
#pragma Table View delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _allSongsNum;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MY_MUSIC_PLAYER_CELL_IDENTIFIER";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [self.musicNameArray objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_currentMusicCount = indexPath.row;
[self musicPlayWithInfoShow];
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath;
}
#pragma mark-
#pragma AVAudioPlayer Delegate
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if (!flag) {
return;
}
if(_currentMusicCount < _allSongsNum - 2)
_currentMusicCount++;
[self musicPlayWithInfoShow];
}