/**************************************************/
/*******************-main文件-***********************/
/**************************************************/
#import <Foundation/Foundation.h>
#import "lrcContent.h"
#import "lrcManager.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
lrcManager *lrcM=[[lrcManager alloc] init];
[lrcM showLrc];
}
return 0;
}
/**************************************************/
/***************-lrcManager.h文件-********************/
/**************************************************/
#import <Foundation/Foundation.h>
#import "lrcContent.h"
#define PATH @"/Users/paul/Desktop/legend.txt"
@interface lrcManager : NSObject
@property NSArray *lrcArray; //存放每行的歌词
-(BOOL)isMessage:(NSString *)lineContent; //判断是否是歌词的其他信息,如歌手名,作词,作曲
-(NSMutableArray *)getLrcFileContent:(NSString *)path; //读取歌词文件内容
-(lrcContent *)polishLrc:(NSMutableArray *)lineArr; //处理单行歌词
-(lrcContent *)sortLrc:(lrcContent *)lrcContent; //对歌词进行排序(根据歌词前的时间)
-(void)showLrc; //显示歌词内容
@end
/**************************************************/
/***************-lrcManager.m文件-********************/
/**************************************************/
#import "lrcManager.h"
@implementation lrcManager
-(id)init
{
if(self=[super init])
{
_lrcC=[[lrcContent alloc] init];
_lrcArray=[[NSArray alloc] init];
_lrcC.lrc=[[NSMutableArray alloc] init];
_lrcC.message=[[NSMutableArray alloc] init];
_lrcC.content=@"";
_lrcC.time=0.0;
}
return self;
}
-(BOOL)isMessage:(NSString *)lineContent
{
int flag=0;
if([lineContent intValue])
{
flag=1;
}
return flag;
}
-(NSMutableArray *)getLrcFileContent:(NSString *)path
{
//将单句歌词分割放入动态数组
NSString *fileContent=[[NSString alloc] initWithContentsOfFile:PATH encoding:NSUTF8StringEncoding error:nil];
NSArray *arrFileContent=[fileContent componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n"]];
NSMutableArray *mArrFileContent=[[NSMutableArray alloc] initWithArray:arrFileContent];
[mArrFileContent removeObject:@""];
return mArrFileContent;
}
-(lrcContent *)polishLrc:(NSString *)lineStr
{
NSArray *arrLine=[lineStr componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"[:]"]];
NSMutableArray *mArrLine=[[NSMutableArray alloc] initWithArray:arrLine];
[mArrLine removeObject:@""];
if(![self isMessage:[mArrLine objectAtIndex:1]])
{
//歌曲信息
[_lrcC.message addObject:[mArrLine objectAtIndex:1]];
}
else
{
//歌词
if([[mArrLine lastObject] intValue]==0)
{
for(int i=0;i<[mArrLine count]-1;i+=2)
{
NSString *s=[[NSString alloc] initWithFormat:@"%f",([[mArrLine objectAtIndex:i] floatValue]*60+[[mArrLine objectAtIndex:i+1] floatValue])];
[_lrcC.lrc addObject:s];
[_lrcC.lrc addObject:[mArrLine lastObject]];
}
}
}
return _lrcC;
}
-(lrcContent *)sortLrc:(lrcContent *)lrcContent
{
NSMutableArray *mArr=[[NSMutableArray alloc] initWithArray:lrcContent.lrc];
for(int i=0;i<[mArr count]-2;i+=2)
{
for(int j=i+2;j<[mArr count];j+=2)
{
if(([[mArr objectAtIndex:j] floatValue])<([[mArr objectAtIndex:i] floatValue]))
{
[mArr exchangeObjectAtIndex:i withObjectAtIndex:j];
[mArr exchangeObjectAtIndex:i+1 withObjectAtIndex:j+1];
}
}
}
lrcContent.lrc=mArr;
return lrcContent;
}
-(void)showLrc
{
lrcManager *lrcM=[[lrcManager alloc] init];
for (int i=0; i<[[self getLrcFileContent:PATH] count]; i++)
{
_lrcC=[lrcM polishLrc:[[lrcM getLrcFileContent:PATH] objectAtIndex:i]];
}
lrcM.lrcC=[lrcM sortLrc:_lrcC];
for(int i=1;i<[_lrcC.lrc count];i+=2)
{
NSLog(@"%@",[_lrcC.lrc objectAtIndex:i]);
sleep(1);
}
}
@end
/**************************************************/
/***************-lrcContent.h文件-********************/
/**************************************************/
#import <Foundation/Foundation.h>
@interface lrcContent : NSObject
@property NSMutableArray *message; //存放歌词的其他信息
@property NSMutableArray *lrc; //存放歌词每行
@property NSString *content; //存放单行歌词
@property float time; //存放歌词的出现时间
@end
/**************************************************/
/***************-lrcContent.m文件-********************/
/**************************************************/
#import "lrcContent.h"
@implementation lrcContent
-(id)init
{
if(self=[super init])
{
_message=[[NSMutableArray alloc] init];
_lrc=[[NSMutableArray alloc] init];
_content=@"";
_time=0.0;
}
return self;
}
@end
/**************************************************/
/*******************-歌词示例文件-********************/
/**************************************************/
[ti:传奇]
[ar:王菲]
[00:03.50]传奇
[00:19.10]作词:刘兵 作曲:李健
[00:20.60]演唱:王菲
[00:26.60]
[04:40.75][02:39.90][00:36.25]只是因为在人群中多看了你一眼
[04:49.00]
[02:47.44][00:43.69]再也没能忘掉你容颜
[02:54.83][00:51.24]梦想着偶然能有一天再相见
[03:02.32][00:58.75]从此我开始孤单思念
[03:08.15][01:04.30]
[03:09.35][01:05.50]想你时你在天边
[03:16.90][01:13.13]想你时你在眼前
[03:24.42][01:20.92]想你时你在脑海
[03:31.85][01:28.44]想你时你在心田
[03:38.67][01:35.05]
[04:09.96][03:39.87][01:36.25]宁愿相信我们前世有约
[04:16.37][03:46.38][01:42.47]今生的爱情故事 不会再改变
[04:24.82][03:54.83][01:51.18]宁愿用这一生等你发现
[04:31.38][04:01.40][01:57.43]我一直在你身旁 从未走远
[04:39.55][04:09.00][02:07.85]
/**************************************************/
/*******************-程序运行示例-********************/
/**************************************************/
2015-08-10 22:16:30.271 myLrcReader[5185:198636] 传奇
2015-08-10 22:16:31.272 myLrcReader[5185:198636] 作词:刘兵作曲:李健
2015-08-10 22:16:32.278 myLrcReader[5185:198636] 演唱:王菲
2015-08-10 22:16:33.281 myLrcReader[5185:198636] 只是因为在人群中多看了你一眼
2015-08-10 22:16:34.283 myLrcReader[5185:198636] 再也没能忘掉你容颜
2015-08-10 22:16:35.288 myLrcReader[5185:198636] 梦想着偶然能有一天再相见
2015-08-10 22:16:36.290 myLrcReader[5185:198636] 从此我开始孤单思念
2015-08-10 22:16:37.296 myLrcReader[5185:198636] 想你时你在天边
2015-08-10 22:16:38.298 myLrcReader[5185:198636] 想你时你在眼前
2015-08-10 22:16:39.298 myLrcReader[5185:198636] 想你时你在脑海
2015-08-10 22:16:40.299 myLrcReader[5185:198636] 想你时你在心田
2015-08-10 22:16:41.303 myLrcReader[5185:198636] 宁愿相信我们前世有约
2015-08-10 22:16:42.308 myLrcReader[5185:198636] 今生的爱情故事不会再改变
2015-08-10 22:16:43.314 myLrcReader[5185:198636] 宁愿用这一生等你发现
2015-08-10 22:16:44.315 myLrcReader[5185:198636] 我一直在你身旁从未走远
2015-08-10 22:16:45.318 myLrcReader[5185:198636] 只是因为在人群中多看了你一眼
2015-08-10 22:16:46.320 myLrcReader[5185:198636] 再也没能忘掉你容颜
2015-08-10 22:16:47.324 myLrcReader[5185:198636] 梦想着偶然能有一天再相见
2015-08-10 22:16:48.329 myLrcReader[5185:198636] 从此我开始孤单思念
2015-08-10 22:16:49.333 myLrcReader[5185:198636] 想你时你在天边
2015-08-10 22:16:50.335 myLrcReader[5185:198636] 想你时你在眼前
2015-08-10 22:16:51.337 myLrcReader[5185:198636] 想你时你在脑海
2015-08-10 22:16:52.343 myLrcReader[5185:198636] 想你时你在心田
2015-08-10 22:16:53.348 myLrcReader[5185:198636] 宁愿相信我们前世有约
2015-08-10 22:16:54.349 myLrcReader[5185:198636] 今生的爱情故事不会再改变
2015-08-10 22:16:55.352 myLrcReader[5185:198636] 宁愿用这一生等你发现
2015-08-10 22:16:56.354 myLrcReader[5185:198636] 我一直在你身旁从未走远
2015-08-10 22:16:57.359 myLrcReader[5185:198636] 宁愿相信我们前世有约
2015-08-10 22:16:58.361 myLrcReader[5185:198636] 今生的爱情故事不会再改变
2015-08-10 22:16:59.366 myLrcReader[5185:198636] 宁愿用这一生等你发现
2015-08-10 22:17:00.369 myLrcReader[5185:198636] 我一直在你身旁从未走远
2015-08-10 22:17:01.372 myLrcReader[5185:198636] 只是因为在人群中多看了你一眼