本次写的是一个简单的音乐播放器;
写播放器首先要下载一首歌, 和歌词, 然后解析你的歌词;把歌词部分和时间部分分别放到数组中.
解析的时候, 要注意有歌词格式的不同, 我下面解析的是一个常见歌词类型如图
HandleDate.h文件
#import <Foundation/Foundation.h>
@interface HandleDate : NSObject
// 用来接收时间的可变数组
@property (nonatomic, retain) NSMutableArray *mArrOfTime;
// 用来接收歌词的可变数组
@property (nonatomic, retain) NSMutableArray *mArrOfLyric;
- (instancetype)init;
- (void)handleLyric;
@end
HandleDate.m文件
#import "HandleDate.h"
@implementation HandleDate
-(instancetype)init {
self = [super init];
if (self) {
_mArrOfTime = [NSMutableArray array];
_mArrOfLyric = [NSMutableArray array];
}
return self;
}
- (void)handleLyric {
// 获取歌词的路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"稻香" ofType:@"lrc"];
// 获取通过路径解析出来的结果
NSString *result = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//时间的格式一般是[00:00.00];的, 所以要以"["分割
NSArray *arrayOfResult = [NSArray array];
arrayOfResult = [result componentsSeparatedByString:@"["];
NSArray *arrayOfLine = [NSArray array];
// 遍历分割后的数组
for (NSString *stringOfline in arrayOfResult) {
// 分割每一行的时间和歌词数组
arrayOfLine = [stringOfline componentsSeparatedByString:@"] "];
// 将时间加入时间数组
[self.mArrOfTime addObject:[arrayOfLine firstObject]];
// 这个分情况, 看自己打印的歌词, 去掉不合适的对象
[self.mArrOfTime removeObject:@""];
// 这个不知道为什么歌词的后面有\n这个符号, 去掉就行, 然后加入歌词数组
if ([arrayOfLine lastObject] != 0) {
NSString *str = [[[arrayOfLine lastObject] componentsSeparatedByString:@"\n"] firstObject];
[self.mArrOfLyric addObject:str];
[self.mArrOfLyric removeObject:@""];
}
}
}
@end
VC.m中(这个里面看重点, 和音乐播放的有关的)
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "HandleDate.h"
//#import "VCOfMV.h"
@interface ViewController ()
// 歌名
@property (nonnull, nonatomic, retain) UILabel *labelOfSongName;
// 下拉箭头
@property (nonatomic, retain) UIButton *buttonOfPull;
// 更多信息
@property (nonatomic, retain) UIButton *buttonOfMore;
// 歌星
@property (nonatomic, retain) UILabel *labelOfName;
// 小按钮
@property (nullable, nonatomic, retain) UIButton *buttonOfStandard;
@property (nullable, nonatomic, retain) UIButton *buttonOfSole;
@property (nullable, nonatomic, retain) UIButton *buttonOfMV;
@property (nullable, nonatomic, retain) UIButton *buttonOfOther;
// 歌名图片
@property (nullable, nonatomic, retain) UIImageView *imageViewOfSong;
// 图片上的button
@property (nonatomic, retain) UIButton *buttonOfWatchMV;
// 歌词
@property (nonatomic, retain) UILabel *labelOfLyric;
// 分页控制器
@property (nullable, nonatomic, retain) UIPageControl *pageControl;
// 进度条
@property (nonnull, nonatomic, retain) UISlider *slider;
// 时间
@property (nonatomic, retain) UILabel *labelOfMin;
@property (nonatomic, retain) UILabel *labelOfMax;
// 暂停, 上/下一首键
@property (nonnull, nonatomic, retain) UIButton *buttonOfLast;
@property (nonnull, nonatomic, retain) UIButton *buttonOfPause;
@property (nonnull, nonatomic, retain) UIButton *buttonOfNext;
// 横屏键
@property (nonatomic, retain) UIButton *buttonOfHorizontal;
// 最下面一栏
@property (nonatomic, retain) UIButton *buttonOfLike;
@property (nonatomic, retain) UIButton *buttonOfType;
@property (nonatomic, retain) UIButton *buttonOfLoad;
@property (nonatomic, retain) UIButton *buttonOfShare;
@property (nonatomic, retain) UIButton *buttonOfListen;
// 播放器
@property (nonatomic, retain) AVAudioPlayer *player;
@property (nonatomic, retain) NSTimer *firstTimer;
@property (nonatomic, retain) HandleDate *handleDate;
@end
@implementation ViewController
- (void)dealloc
{
[_labelOfSongName release];
[_buttonOfPull release];
[_buttonOfMore release];
[_labelOfName release];
[_buttonOfStandard release];
[_buttonOfSole release];
[_buttonOfMV release];
[_buttonOfOther release];
[_imageViewOfSong release];
[_buttonOfWatchMV release];
[_labelOfLyric release];
[_pageControl release];
[_slider release];
[_buttonOfLast release];
[_buttonOfPause release];
[_buttonOfNext release];
[_buttonOfHorizontal release];
[_buttonOfLike release];
[_buttonOfShare release];
[_buttonOfLoad release];
[_buttonOfType release];
[_buttonOfListen release];
[_player release];
[_firstTimer release];
[_labelOfMin release];
[_labelOfMax release];
[_handleDate release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *imageViewOfBackGround = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imageViewOfBackGround.image = [UIImage imageNamed:@"player_albumblur_default"];
[self.view addSubview:imageViewOfBackGround];
[imageViewOfBackGround release];
[self createSubViews];
[self createPlayer];
}
- (void)createSubViews {
// 下拉箭头
_buttonOfPull = [UIButton buttonWithType:UIButtonTypeCustom];
_buttonOfPull.frame = CGRectMake(10, 30, 40, 40);
[_buttonOfPull setImage:[UIImage imageNamed:@"miniplayer_btn_playlist_close-1"] forState:UIControlStateNormal];
[self.view addSubview:_buttonOfPull];
// 歌名
_labelOfSongName = [[UILabel alloc] initWithFrame:CGRectMake(50, 30, self.view.frame.size.width - 100, 40)];
[self.view addSubview:_labelOfSongName];
[_labelOfSongName release];
_labelOfSongName.text = @"稻香";
_labelOfSongName.textColor = [UIColor whiteColor];
_labelOfSongName.textAlignment = NSTextAlignmentCenter;
_labelOfSongName.font = [UIFont systemFontOfSize:20.0f];
// 更多设置
_buttonOfMore = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:_buttonOfMore];
_buttonOfMore.frame = CGRectMake(self.view.frame.size.width - 50, 30, 40, 40);
[_buttonOfMore setImage:[UIImage imageNamed:@"main_tab_more"] forState:UIControlStateNormal];
// 歌星
_labelOfName = [[UILabel alloc] initWithFrame:CGRectMake(100, 70, self.view.frame.size.width - 200, 30)];
[self.view addSubview:_labelOfName];
[_labelOfName release];
_labelOfName.textAlignment = NSTextAlignmentCenter;
_labelOfName.textColor = [UIColor whiteColor];
_labelOfName.text = @"—— 周杰伦 ——";
// 标签栏
_buttonOfStandard = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:_buttonOfStandard];
_buttonOfStandard.frame = CGRectMake(75, 110, 60, 30);
_buttonOfStandard.backgroundColor = [UIColor redColor];
[_buttonOfStandard setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
_buttonOfSole = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:_buttonOfSole];
_buttonOfSole.frame = CGRectMake(145, 110, 60, 30);
_buttonOfSole.backgroundColor = [UIColor redColor];
[_buttonOfSole setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
_buttonOfMV = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:_buttonOfMV];
_buttonOfMV.frame = CGRectMake(215, 110, 60, 30);
_buttonOfMV.backgroundColor = [UIColor redColor];
[_buttonOfMV setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
_buttonOfOther = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:_buttonOfOther];
_buttonOfOther.frame = CGRectMake(285, 110, 60, 30);
_buttonOfOther.backgroundColor = [UIColor redColor];
[_buttonOfOther setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
// 中间圆图
_imageViewOfSong = [[UIImageView alloc] initWithFrame:CGRectMake(70, 160, self.view.frame.size.width - 140, self.view.frame.size.width - 140)];
[self.view addSubview:_imageViewOfSong];
[_imageViewOfSong release];
_imageViewOfSong.backgroundColor = [UIColor cyanColor];
[_imageViewOfSong setImage:[UIImage imageNamed:@"song"]];
_imageViewOfSong.layer.masksToBounds = YES;
_imageViewOfSong.layer.cornerRadius = _imageViewOfSong.frame.size.width / 2;
_imageViewOfSong.layer.borderWidth = 10;
_imageViewOfSong.layer.borderColor = [UIColor colorWithRed:0.16 green:0.11 blue:0.05 alpha:0.8].CGColor;
// 歌词栏
_labelOfLyric = [[UILabel alloc] initWithFrame:CGRectMake(50, 435, self.view.frame.size.width - 100, 40)];
[self.view addSubview:_labelOfLyric];
[_labelOfLyric release];
_labelOfLyric.text = @"曲: 周杰伦";
_labelOfLyric.textColor = [UIColor whiteColor];
_labelOfLyric.textAlignment = NSTextAlignmentCenter;
// 观看MV按钮
_buttonOfWatchMV = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:_buttonOfWatchMV];
_buttonOfWatchMV.frame = CGRectMake(150, 380, self.view.frame.size.width - 300, 40);
[_buttonOfWatchMV setTitle:@"观看MV" forState:UIControlStateNormal];
[_buttonOfWatchMV setTintColor:[UIColor whiteColor]];
[_buttonOfWatchMV addTarget:self action:@selector(watchMV:) forControlEvents:UIControlEventTouchUpInside];
// 分段控制器
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 475, self.view.frame.size.width - 200, 20)];
[self.view addSubview:_pageControl];
[_pageControl release];
_pageControl.numberOfPages = 3;
_pageControl.currentPage = 1;
// 进度条
_slider = [[UISlider alloc] initWithFrame:CGRectMake(50, 520, self.view.frame.size.width - 100, 20)];
[self.view addSubview:_slider];
[_slider release];
[_slider setMinimumTrackImage:[UIImage imageNamed:@"player_slider_playback_left"] forState:UIControlStateNormal];
[_slider setMaximumTrackImage:[UIImage imageNamed:@"player_slider_playback_right"] forState:UIControlStateNormal];
[_slider setThumbImage:[UIImage imageNamed:@"player_slider_playback_thumb"] forState:UIControlStateNormal];
// 进度条两边的时间
self.labelOfMin = [[UILabel alloc] initWithFrame:CGRectMake(0, 520, 50, 20)];
[self.view addSubview:_labelOfMin];
[_labelOfMin release];
_labelOfMin.textColor = [UIColor whiteColor];
_labelOfMin.textAlignment = NSTextAlignmentCenter;
self.labelOfMax = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 50, 520, 50, 20)];
[self.view addSubview:_labelOfMax];
[_labelOfMax release];
_labelOfMax.textColor = [UIColor whiteColor];
_labelOfMax.textAlignment = NSTextAlignmentCenter;
// 上一曲
_buttonOfLast = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:_buttonOfLast];
_buttonOfLast.frame = CGRectMake(70, 570, 50, 50);
_buttonOfLast.backgroundColor = [UIColor grayColor];
_buttonOfLast.layer.cornerRadius = 25;
// 暂停/播放键
_buttonOfPause = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:_buttonOfPause];
_buttonOfPause.frame = CGRectMake(170, 560, self.view.frame.size.width - 340, self.view.frame.size.width - 340);
_buttonOfPause.backgroundColor = [UIColor grayColor];
_buttonOfPause.layer.cornerRadius = (self.view.frame.size.width - 340) / 2;
[_buttonOfPause setTitle:@"播放" forState:UIControlStateNormal];
[_buttonOfPause addTarget:self action:@selector(playSong:) forControlEvents:UIControlEventTouchUpInside];
// 下一曲
_buttonOfNext = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:_buttonOfNext];
_buttonOfNext.frame = CGRectMake(self.view.frame.size.width - 120, 570, 50, 50);
_buttonOfNext.backgroundColor = [UIColor grayColor];
_buttonOfNext.layer.cornerRadius = 25;
// 横屏键
_buttonOfHorizontal = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:_buttonOfHorizontal];
_buttonOfHorizontal.frame = CGRectMake(self.view.frame.size.width - 50, 580, 40, 30);
_buttonOfHorizontal.backgroundColor = [UIColor grayColor];
// 最小面的按钮
_buttonOfLike = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:_buttonOfLike];
_buttonOfLike.frame = CGRectMake(0, self.view.frame.size.height - 70, self.view.frame.size.width / 5, 70);
_buttonOfLike.backgroundColor = [UIColor purpleColor];
[_buttonOfLike setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
_buttonOfType = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:_buttonOfType];
_buttonOfType.frame = CGRectMake(self.view.frame.size.width * 1 / 5, self.view.frame.size.height - 70, self.view.frame.size.width / 5, 70);
_buttonOfType.backgroundColor = [UIColor cyanColor];
[_buttonOfType setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
_buttonOfLoad = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:_buttonOfLoad];
_buttonOfLoad.frame = CGRectMake(self.view.frame.size.width * 2 / 5, self.view.frame.size.height - 70, self.view.frame.size.width / 5, 70);
_buttonOfLoad.backgroundColor = [UIColor greenColor];
[_buttonOfLoad setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
_buttonOfShare = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:_buttonOfShare];
_buttonOfShare.frame = CGRectMake(self.view.frame.size.width * 3 / 5, self.view.frame.size.height - 70, self.view.frame.size.width / 5, 70);
_buttonOfShare.backgroundColor = [UIColor orangeColor];
[_buttonOfShare setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
_buttonOfListen = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:_buttonOfListen];
_buttonOfListen.frame = CGRectMake(self.view.frame.size.width * 4 / 5, self.view.frame.size.height - 70, self.view.frame.size.width / 5, 70);
_buttonOfListen.backgroundColor = [UIColor whiteColor];
[_buttonOfListen setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
}
#pragma mark ------------ 观看MV -----------
- (void)watchMV:(UIButton *)button {
VCOfMV *vc = [[VCOfMV alloc] init];
[self presentViewController:vc animated:YES completion:^{
}];
[self.player pause];
}
- (void)createPlayer {
#pragma mark ------------ 创建播放器 -----------
NSString *path = [[NSBundle mainBundle] pathForResource:@"周杰伦 - 稻香" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:path];
self.player = [[[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil] autorelease];
/** slider的设置 */
_slider.minimumValue = 0.0f;
_slider.maximumValue = self.player.duration;
[_slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
// 定时器 让slider随player播放移动
self.firstTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeTime:) userInfo:nil repeats:YES];
// 进度条两边的时间
_labelOfMin.text = @"00:00";
NSInteger allTime = self.player.duration;
if (allTime / 60 < 10) {
_labelOfMax.text = [NSString stringWithFormat:@"0%ld:%ld", allTime / 60, allTime % 60];
} else if (allTime / 60 > 10) {
_labelOfMax.text = [NSString stringWithFormat:@"%ld:%ld", allTime / 60, allTime % 60];
}
}
// 暂停和播放
- (void)playSong:(UIButton *)button {
[self.player play];
[button removeTarget:self action:@selector(playSong:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(pauseSong:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"暂停" forState:UIControlStateNormal];
}
- (void)pauseSong:(UIButton *)button {
[self.player pause];
[button removeTarget:self action:@selector(pauseSong:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(playSong:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"播放" forState:UIControlStateNormal];
}
// 进度条
- (void)sliderAction:(UISlider *)slider {
self.player.currentTime = self.slider.value;
}
// 定时器
- (void)changeTime:(NSTimer *)timer {
self.slider.value = self.player.currentTime;
// 时间跟着走
NSInteger currentTime = self.player.currentTime;
if (currentTime / 60 < 10) {
if (currentTime % 60 < 10) {
_labelOfMin.text = [NSString stringWithFormat:@"0%ld:0%ld", currentTime / 60, currentTime % 60];
} else if (currentTime % 60 > 10) {
_labelOfMin.text = [NSString stringWithFormat:@"0%ld:%ld", currentTime / 60, currentTime % 60];
}
} else if (currentTime / 60 > 10) {
if (currentTime % 60 < 10) {
_labelOfMin.text = [NSString stringWithFormat:@"%ld:0%ld", currentTime / 60, currentTime % 60];
} else if (currentTime % 60 > 10) {
_labelOfMin.text = [NSString stringWithFormat:@"%ld:%ld", currentTime / 60, currentTime % 60];
}
}
self.handleDate = [[[HandleDate alloc] init] autorelease];
[self.handleDate handleLyric];
for (int i = 0; i < self.handleDate.mArrOfTime.count; i++) {
NSArray *arr = [self.handleDate.mArrOfTime[i] componentsSeparatedByString:@":"];
int lyricTime = [[arr firstObject] intValue] * 60 + [[arr lastObject] intValue];
int stardand = self.player.currentTime;
if (lyricTime == stardand) {
_labelOfLyric.text = _handleDate.mArrOfLyric[i];
}
}
}
#pragma mark ------------ 改变状态栏的颜色 -----------
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end