歌词解析习题

 
1 #import <Foundation/Foundation.h>
2 #import "LRCManager.h"
3 int main(int argc, const char * argv[]) {
4     @autoreleasepool {
5         [LRCManager test];
6     }
7     return 0;
8 }
main.m
 1 #import <Foundation/Foundation.h>
 2 #import "Lrc.h"
 3 #import "LRCMessage.h"
 4 @interface LRCManager : NSObject{
 5     //数组用来存放 Lrc类的对象
 6     NSMutableArray * _array;
 7 }
 8 @property LRCMessage * message; 
 9 //构造方法
10 - (id)init;
11 //解析歌词
12 - (void)analyseLrcFile;
13 //对单行进行解析 获得Lrc类的对象并存入数组
14 - (void)parseLrcEach:(NSString *)str;
15 //测试方法
16 + (void)test;
17 //传入时间 返回当前时间播放的歌词
18 - (NSString *)lrcForTime:(float)time;
19 - (void)sortArray;
20 
21 @end
LRCManager.h
  1 #import "LRCManager.h"
  2 #define PATH @"/Users/Gao/Desktop/legend.txt"
  3 @implementation LRCManager
  4 
  5 - (id)init{
  6     if (self = [super init]) {
  7         //分配空间初始化
  8         _array = [[NSMutableArray alloc]init];
  9         _message = [[LRCMessage alloc]init];
 10         //解析
 11     
 12         [self analyseLrcFile];
 13     }
 14     return self;
 15 }
 16 
 17 - (void)analyseLrcFile{
 18     //从文件中获取到字符串
 19     NSString * lrcStr = [NSString stringWithContentsOfFile:PATH encoding:NSUTF8StringEncoding error:nil];
 20     //字符串用"
"分割 用lrcEach数组接收 获得每行的字符串
 21     NSArray * lrcEach = [lrcStr componentsSeparatedByString:@"
"];
 22     //遍历数组
 23     for (NSString * lrc  in lrcEach) {
 24         //如果该字符串是以"[0"开头 则为歌词 进行解析
 25         if ([lrc hasPrefix:@"[0"]) {
 26             [self parseLrcEach:lrc];
 27         }
 28         //如果改字符串是以[开头 则为歌曲信息
 29         else if ([lrc hasPrefix:@"["]){
 30             NSCharacterSet * set = [NSCharacterSet characterSetWithCharactersInString:@"[:]"];
 31             NSArray * messageArray = [lrc componentsSeparatedByCharactersInSet:set];
 32             
 33             if ([messageArray[1] isEqualToString:@"ti"]) {
 34                 _message.ti = messageArray[2];
 35             }else if ([messageArray[1] isEqualToString:@"ar"]){
 36                 _message.ar = messageArray[2];
 37             }else if ([messageArray[1] isEqualToString:@"al"]){
 38                 _message.al = messageArray[2];
 39             }else if ([messageArray[1] isEqualToString:@"by"]){
 40                 _message.by = messageArray[2];
 41             }
 42         }
 43     }
 44     
 45 }
 46 
 47 //传入字符串进行解析
 48 - (void)parseLrcEach:(NSString *)str{
 49     //定义字符集合"[:";
 50     NSCharacterSet * set = [NSCharacterSet characterSetWithCharactersInString:@"[:"];
 51     //把传入的字符用"]"分割   分割得来的字符串用数组arr接收
 52     NSArray * arr = [str componentsSeparatedByString:@"]"];
 53     //遍历数组arr
 54     for (NSString * str in arr) {
 55         //当数组中的元素以"["开头 则说明是时间 开始进行seconds的计算
 56         if ([str hasPrefix:@"["]) {
 57             //用上面定义的字符集和来分割该字符串 并用timeArray数组接收
 58             //此时分割下的格式为  ""|分|秒
 59             NSArray * timeArray = [str componentsSeparatedByCharactersInSet:set];
 60             //第0个元素为空 第1个元素为分 第二个元素为秒
 61             float minute = [timeArray[1] floatValue];
 62             float second = [timeArray[2] floatValue];
 63             //求得seconds
 64             float seconds = minute * 60 + second;
 65             //创建一个Lrc类的对象 lrc
 66             Lrc * lrc = [[Lrc alloc]init];
 67             //设置该歌词的时间
 68             lrc.seconds = seconds;
 69             //arr数组的最后一个元素一定是歌词 设置改对象的歌词属性
 70             lrc.lrc = [arr lastObject];
 71             //把该lrc对象添加到_array中
 72             [_array addObject:lrc];
 73         }
 74         
 75     }
 76 }
 77 - (void)sortArray{
 78     //数组排序  按照数组中对象的seconds属性升序排列 歌词从头到尾排好
 79     [_array sortedArrayUsingComparator:^NSComparisonResult(Lrc * obj1, Lrc * obj2) {
 80         return obj1.seconds > obj2.seconds;
 81     }];
 82 //    for (int i = 0; i<_array.count; i++) {
 83 //        for (int j = 0; j<_array.count-i-1; i++) {
 84 //
 85 //        }
 86 //    }
 87 }
 88 
 89 - (NSString *)lrcForTime:(float)time{
 90     //传入时间点 返回该时间点的歌词  从最后开始向前遍历 直到time比对象的seconds大 则返回该对象的歌词属性
 91     for (NSInteger i = _array.count-1 ; i>=0; i--) {
 92         if (time > [_array[i] seconds]) {
 93             return [_array[i] lrc];
 94         }
 95     }
 96     return nil;
 97     
 98 
 99 }
100 
101 
102 +(void)test{
103     //测试
104     LRCManager * manager = [[LRCManager alloc]init];
105     int i = 0;
106     while (++i<240) {
107         system("clear");
108         NSLog(@"%@
%@",manager.message, [manager lrcForTime:i]);
109         //NSLog(@"%@",[manager lrcForTime:i]);
110         //1秒打印一次
111         sleep(1);
112     }
113 }
114 
115 
116 @end
LRCManager.m
 1 @interface Lrc : NSObject
 2 //时间属性
 3 @property float seconds;
 4 
 5 //歌词属性
 6 @property NSString * lrc;
 7 
 8 
 9 
10 @end
Lrc.h
1 #import "Lrc.h"
2 
3 @implementation Lrc
4 
5 
6 @end
Lrc.m
1 #import <Foundation/Foundation.h>
2 
3 @interface LRCMessage : NSObject
4 //歌词信息类
5 @property NSString * ti;
6 @property NSString * ar;
7 @property NSString * al;
8 @property NSString * by;
9 @end
LRCMessage.h
1 #import "LRCMessage.h"
2 
3 @implementation LRCMessage
4 //重写description方法 打印对象时输出
5 - (NSString *)description
6 {
7     return [NSString stringWithFormat:@"
 ti:%@
 ar:%@
 al:%@
 by:%@ ", _ti,_ar,_al,_by];
8 }
9 @end
LRCMessage.m
 
 
歌词文件 

原文地址:https://www.cnblogs.com/gwkiOS/p/4934639.html