ios开发学习- 简易音乐播放器2 (基于iPhone4s屏幕尺寸)-- 歌词解析--plist文件应用--imageNamed图片加载耗内存

声明:(部分图片来自网络,如果侵犯了您的权益请联系我,会尽快删除!)

又是音乐播放器,不过这次和上次不一样了,准确说这次更像播放器了,初学者不建议看这个,可以先看前面一个音乐播放器(1),当然 我没加1,写了这个,就把前面的默认当1吧

先上图:

ios开发学习-  简易音乐播放器2 (基于iPhone4s屏幕尺寸)-- 歌词解析--plist文件应用--imageNamed图片加载耗内存

接下来源码:(一样:大量的三元式,多看看就习惯了,主要是习惯一行能干的事绝不用两行);

 //
// ViewController.m
// C_MusicPlayer
//
// Created by Ibokan on 15/8/22.
// Copyright (c) 2015年 Crazy凡. All rights reserved.
// #import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "LyricsAnalysis.h" @interface ViewController () <AVAudioPlayerDelegate>
@property (nonatomic,strong)UIImageView *imageview;//bgimg
@property (nonatomic,strong)UISlider *slider;//播放进度条
@property (nonatomic,strong)UILabel *tlableall;//音乐总时长
@property (nonatomic,strong)UILabel *tlablecur;//音乐当前播放时长
@property (nonatomic,strong)UILabel *labellrc;//歌词显示label @property (nonatomic,strong)UIButton *buttonPlayandPause;
@property (nonatomic,strong)UIButton *buttonPrevious;
@property (nonatomic,strong)UIButton *buttonNext;
@property (nonatomic,strong)AVAudioPlayer *player;
@property (nonatomic,strong)LyricsAnalysis *lrc;
@property (nonatomic,strong)UIImageView *musicImg;
@property (nonatomic,strong)NSTimer *timer;//计时器
@property (nonatomic,strong)NSMutableArray *musicListArray;//歌曲列表 @property int indexLrc;//歌词播放指向
@property int indexMusicList;//歌曲播放指向 //@property (nonatomic,strong)UIScrollView * scrollview;
@end @implementation ViewController - (NSArray *)musicListArray//初始化歌曲列表 : 懒加载
{
if(!_musicListArray)
{
_musicListArray = [NSMutableArray arrayWithArray:[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Music.plist" ofType:nil]]];
for(NSDictionary *temp in _musicListArray)//屏蔽无效路径
{
[[NSBundle mainBundle]pathForResource:[temp valueForKey:@"music"] ofType:nil] ? nil:[_musicListArray removeObject:temp];
}
}
return _musicListArray;
} - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. self.indexLrc = ;
self.indexMusicList = ; self.musicImg = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];//初始化歌曲图片
self.musicImg.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.musicImg.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:self.musicImg]; self.imageview = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];//初始化背景
[self.imageview setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"bgimg.png" ofType:nil]]];
self.imageview.backgroundColor = [UIColor colorWithRed: green: blue: alpha:];
self.imageview.contentMode = UIViewContentModeScaleAspectFill;
self.imageview.userInteractionEnabled = true;
[self.view addSubview:self.imageview]; self.slider = [[UISlider alloc]initWithFrame:CGRectMake(, , , )];//初始化进度条
self.slider.value = 0.0;
[self.view addSubview:self.slider];
[self.slider addTarget:self action:@selector(updateValue) forControlEvents:UIControlEventValueChanged]; self.labellrc = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];//初始化歌词显示label
self.labellrc.textColor = [UIColor whiteColor];
self.labellrc.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.2];
self.labellrc.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.labellrc]; self.tlablecur = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];//初始化当前播放时间显示label
self.tlablecur.textColor = [UIColor whiteColor];
self.tlablecur.text = @"00:00";
self.tlablecur.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.tlablecur]; self.tlableall = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];//初始化音乐总时间显示label
self.tlableall.textColor = [UIColor whiteColor];
self.tlableall.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.tlableall]; self.buttonPlayandPause = [[UIButton alloc]initWithFrame:CGRectMake(, ,, )];//初始化并插入开始and暂停按钮
[self.view addSubview:self.buttonPlayandPause];
[self.buttonPlayandPause setBackgroundImage:[UIImage imageNamed:@"start.png"] forState:UIControlStateNormal];
[self.buttonPlayandPause addTarget:self action:@selector(startandpause) forControlEvents:UIControlEventTouchUpInside]; self.buttonPrevious = [[UIButton alloc]initWithFrame:CGRectMake(, , , )];//初始化并插入上一曲按钮
[self.view addSubview:self.buttonPrevious];
[self.buttonPrevious setBackgroundImage:[UIImage imageNamed:@"previous.png"] forState:UIControlStateNormal];
[self.buttonPrevious addTarget:self action:@selector(previous) forControlEvents:UIControlEventTouchUpInside]; self.buttonNext = [[UIButton alloc]initWithFrame:CGRectMake(, , , )];//初始化并插入下一曲按钮
[self.view addSubview:self.buttonNext];
[self.buttonNext setBackgroundImage:[UIImage imageNamed:@"next.png"] forState:UIControlStateNormal];
[self.buttonNext addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside]; [self addSource];//歌词 歌曲 图片 初始化
}
- (void)startandpause//设置按钮选项
{
if([self.player isPlaying])
{
[self.timer invalidate];
[self.player pause];
[self.buttonPlayandPause setBackgroundImage:[UIImage imageNamed:@"start.png"] forState:UIControlStateNormal];
}
else
{
[self.player play];
[self.buttonPlayandPause setBackgroundImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(timeadd) userInfo:nil repeats:YES];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(rotate) userInfo:nil repeats:YES];
}
}
- (void)playerclear//停止 管理所有的清零事件
{
self.indexLrc = ;//歌词指示变0
[self.player stop];//播放器停止
self.player.currentTime = ;//当前播放时间停止
[self.timer invalidate];//timer 停止
self.slider.value = ;//进度条清零
self.tlablecur.text = @"00:00";
self.labellrc.text = [NSString stringWithFormat:@"%@ : %@",self.lrc.ar,self.lrc.ti]; //歌词显示成标题+作者
}
- (void)addSource//更改歌曲资源
{
self.player= [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]pathForResource:[[self.musicListArray objectAtIndex:self.indexMusicList]valueForKey:@"music"] ofType:nil]]error:nil];//歌曲初始化
if(self.player == nil) //防止加载不到音乐文件
{
[self next] ;
return;
}
self.player.delegate = self;//设置委托回调
self.lrc = [[LyricsAnalysis alloc]initWithFileName:[[self.musicListArray objectAtIndex:self.indexMusicList]valueForKey:@"lyrics"] ofType:nil];//歌词初始化
NSString *imgUrlTemp = [[NSBundle mainBundle]pathForResource:[[self.musicListArray objectAtIndex:self.indexMusicList]valueForKey:@"picture"]ofType:nil];//检查图片路径
[self.musicImg setImage:[UIImage imageWithContentsOfFile:(imgUrlTemp ? imgUrlTemp : [[NSBundle mainBundle]pathForResource:@"noImg.png" ofType:nil])]];//设置图片
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];//设置总时间
[formatter setDateFormat:@"mm:ss"];
self.tlableall.text = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:self.player.duration]];
self.labellrc.text = [NSString stringWithFormat:@"%@ - %@", self.lrc.ti , self.lrc.ar];//设置第一句歌词
}
- (void)previous//上一首
{
self.indexMusicList == ? self.indexMusicList = self.musicListArray.count - : self.indexMusicList-- ;//第一首自动切换到最后一首
[self playerclear];
[self addSource];
[self startandpause];
}
- (void)next//下一首
{
self.indexMusicList == (self.musicListArray.count - ) ? (self.indexMusicList = ) : self.indexMusicList++;//最后一首自动切换到第一首
[self playerclear];
[self addSource];
[self startandpause];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag//播放结束后 播放下一首
{
flag ? [self next]:nil;
}
- (void)timeadd//更新滚动条
{
self.slider.value = self.player.currentTime/self.player.duration;//进度条工作
[self setTLableCur];
self.indexLrc == self.lrc.lrcArrayTime.count ? self.labellrc.text = self.lrc.lrcArrayStr[self.indexLrc - ]:fabs([self.lrc.lrcArrayTime[self.indexLrc] doubleValue] - self.player.currentTime) < 0.06?self.labellrc.text = self.lrc.lrcArrayStr[self.indexLrc++]:nil; //判断歌词是不是全部播放完了,是就显示:最后一句歌词 不是 就判断是不是有哪句歌词是当前应该播放的(时间差小于0.05s)有就设置播放,没有就不做任何处理
}
-(void)rotate//图片旋转
{
CGAffineTransform t=CGAffineTransformRotate(self.musicImg.transform, 0.01);
self.musicImg.transform=t;
}
- (void)updateValue//拖动进度条动作
{
self.player.currentTime = self.player.duration * self.slider.value;
[self setTLableCur];
self.indexLrc = ;//歌词数组遍历完成了 或者 当前指向的歌词的播放时间 比 当前歌曲播放时间大;就跳出循环
while(self.indexLrc != self.lrc.lrcArrayTime.count && [self.lrc.lrcArrayTime[self.indexLrc] doubleValue] < self.player.currentTime)
{
self.indexLrc++;
}
self.labellrc.text = self.lrc.lrcArrayStr[self.indexLrc->-?self.indexLrc-:];//跳出循环的 就播放当前指向的前一句歌词
}
- (void)setTLableCur//设置当前播放时间
{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"mm:ss"]; //格式化时间配置
self.tlablecur.text = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:self.player.currentTime]]; //设置显示时间
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

知识点解析:

1、

#import "LyricsAnalysis.h"

自己手写的歌词解析类,可以查看我的上一篇博客去看看实现,记得按照下面的更新更新类里面的修改部分;

2、懒加载,用了下,感觉还行,具体的不会细讲,反正就是一种不用你管的自动运行的机制,想详细了解的同学,自己看,我只是声明下,以免有没见过的看见不认识,

3、

CGAffineTransform t=CGAffineTransformRotate(self.musicImg.transform, 0.01);

self.musicImg.transform=t;

实现图片旋转,系统提供的类,

4、图片遮盖实现免切图的圆形;

音乐播放的时候的图片,切图太麻烦,巧妙地利用背景图,将背景图置于歌曲专辑图的上方,留下一块圆形空白,于是看到的就是圆形图片了,很容易理解的逻辑

5、歌词匹配的基本原理,是歌词应该播放的时间与当前歌曲播放的时间的差的绝对值足够小

6、拖动进度条歌词从第一句开始匹配,一直找到适当的位置为止,

7、plist  文件应用。

由于博客园上传文件大小限制,此处连接为百度云盘连接  不保证持续有效

点我下载源码

上一篇:如何处理JSON中的特殊字符


下一篇:Android 保存联系人,包括部门\职位\传真\地址\照片