该版本选择通过正则表达式来实现,只需一行正则表达式,即可解决一切问题。
代码中日期正则表达式:
"\\d{1,2}:\\d{2}.\\d{2}"
当日期正则在当前匹配成功,则可断定为合法行。
通过以下代码来获取到当前行的歌词内容:
NSRange lastRange = [[matchs lastObject] rangeAtIndex:0];
NSString *lrcc = [lineText substringFromIndex:lastRange.location + lastRange.length + 1];
以前版本在解析时,使用字符串截取来解析歌词,由于lrc格式多样化,很容易导致异常。
原理:
核心代码只有MusicLrcView.h/m
和MusicLrcParser.h/m
四个文件
将直接拷贝到项目中,然后import "MusicLrcView.h"
使用即可
在混编编译过程中,出现内核支持问题。(暂时废弃)
直接下载MusicLRC.framework
,导入到项目中。
#import <MusicLRC/MusicLrcView.h>
pod 'MusicLrc', :git => 'https://github.com/it-boyer/MusicLrcTest.git'
然后,在项目目录pod安装
$ pod install
-
加入你的pod本地私有库中:
$ pod repo add PodRepo https://github.com/it-boyer/PodRepo.git
-
在终端查询
MusicLrc
$ pod search MusicLrc
-> MusicLrc (0.0.2) 使用正则表达式,重新定义解析歌词的算法 pod 'MusicLrc', '~> 3.2' - Homepage: https://github.com/it-boyer/MusicLrcTest - Source: https://github.com/it-boyer/MusicLrcTest.git - Versions: 3.2, 3.0, 2.0, 0.0.2 [podRepo repo]
-
配置Podfile文件: source 'https://github.com/it-boyer/PodRepo.git'
pod 'MusicLrc', '~> 3.2'
-
在项目根目录安装
$ pod install
+(MusicLrcView *)shared;
用于初始化显示的歌词页面,其中具体实现是通过tableView
相关接口,来实现显示和用户的相关交互功能。
[self.view addSubview:[MusicLrcView shared]];
功能:
- 用于初始化界面之后,加载歌词到歌词界面上。
- 在切换歌曲时,同步切换歌词 调用方式:
-(void)loadLrcBy:(NSString *)lrcPath audioPlayer:(AVAudioPlayer *)player lrcDedegate:(id<MusicLrcDelegate>)lrcDelegate;
lrcPath
: lrc格式歌词路径
audioPlayer
:AVAudioPlayer
播放器实例
lrcDelegate
:遵循MusicLrcDelegate
协议的类
//代理
@protocol MusicLrcDelegate <NSObject>
//重设高亮歌词颜色
-(UIColor *)musicLrcHighlightColor;
-(UIColor *)musicLrcColor;
-(UIImage *)visualEffectImage;
-(BOOL)refreshAllLrcColor;
-(void)refreshFinish;
@end
具体过程:
//添加音频路径
MusicLrcView *lrcView = [MusicLrcView shared];
[[MusicLrcView shared] loadLrcBy:@"荷塘月色" audioPlayer:_audioPlayer lrcDedegate:self];
[self.view addSubview:lrcView];
HttpClientManager是使用Swift编写,需要注意-Swift.h映射文件
[HttpClientManager.shareInstance loadLrcByLrcModel:lrcmodel player:_audioPlayer lrcDelegate:self completion:^(BOOL finished,NSString * loginfo) {
}];