[关闭]
@fiy-fish 2015-07-18T20:12:48.000000Z 字数 2176 阅读 1487

day07-01-歌词解析

Objective-c


  1. //[海阔天空+歌词][1]
  2. //密码:S51T
  3. //[海阔天空:歌词][2]
  4. // main.m
  5. // day07-01-歌词解析
  6. //
  7. // Created by Aaron on 15/7/9.
  8. // Copyright (c) 2015年 Aaron. All rights reserved.
  9. //
  10. #import <Foundation/Foundation.h>
  11. #import "LrcManager.h"
  12. int main(int argc, const char * argv[]) {
  13. @autoreleasepool {
  14. LrcManager *manager = [[LrcManager alloc] init];
  15. [manager readLrcFile:@"/Users/qianfeng/Desktop/海阔天空.txt"];
  16. NSLog(@"%@",[manager searchLrcForTime:@"00:00.08"]);
  17. }
  18. return 0;
  19. }

  1. #import <Foundation/Foundation.h>
  2. @interface LrcManager : NSObject
  3. {
  4. NSMutableArray *_timesArray;
  5. NSMutableDictionary *_timeAndLrcDic;
  6. }
  7. -(void)readLrcFile:(NSString *)path;
  8. -(NSString *)searchLrcForTime:(NSString *)timeStr;
  9. @end

  1. #import "LrcManager.h"
  2. @implementation LrcManager
  3. -(instancetype)init
  4. {
  5. if(self = [super init])
  6. {
  7. _timesArray = [NSMutableArray array];
  8. _timeAndLrcDic = [NSMutableDictionary dictionary];
  9. }
  10. return self;
  11. }
  12. -(void)readLrcFile:(NSString *)path
  13. {
  14. NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  15. //NSLog(@"%@",str);
  16. //按行切割
  17. NSArray *lines = [str componentsSeparatedByString:@"\n"];
  18. //解析每一行
  19. [self pareAllLines:lines];
  20. }
  21. -(void)pareAllLines:(NSArray *)lines
  22. {
  23. for(NSString *oneLine in lines)
  24. {
  25. NSArray *array = [oneLine componentsSeparatedByString:@"]"];
  26. // NSLog(@"---------");
  27. NSLog(@"%ld",array.count);
  28. NSLog(@"%@",array);
  29. // NSLog(@"---------");
  30. //[00:00.33 海阔天空
  31. //判断是否有歌词
  32. if(array.count > 1)
  33. {
  34. //求毫秒
  35. NSInteger mSecond = [self returnMSecond:[array[0] substringFromIndex:1]];
  36. //存储关系
  37. [_timeAndLrcDic setObject:array[1] forKey:@(mSecond)];
  38. [_timesArray addObject:@(mSecond)];
  39. }
  40. }
  41. // NSLog(@"%@",_timeAndLrcDic);
  42. // NSLog(@"%@",_timesArray);
  43. }
  44. // 00:00.33
  45. -(NSInteger)returnMSecond:(NSString *)timeStr
  46. {
  47. NSInteger minute = [[timeStr substringToIndex:2] integerValue];
  48. NSInteger second = [[timeStr substringWithRange:NSMakeRange(3, 2)] integerValue];
  49. NSInteger mSecond = [[timeStr substringFromIndex:6] integerValue];
  50. return (minute*60+second)*100+mSecond;
  51. }
  52. -(NSString *)searchLrcForTime:(NSString *)timeStr
  53. {
  54. NSInteger mSecond = [self returnMSecond:timeStr];
  55. //查找数组里合适的时间点
  56. for(int i = 0 ; i < [_timesArray count]; i++)
  57. {
  58. NSNumber *time = _timesArray[i];
  59. if(mSecond < [time integerValue])
  60. {
  61. if(i == 0)
  62. {
  63. return nil;
  64. }
  65. else
  66. {
  67. NSString *lrc = [_timeAndLrcDic objectForKey:_timesArray[i-1]];
  68. return lrc;
  69. }
  70. }
  71. if(i == [_timesArray count]-1)
  72. {
  73. return nil;
  74. }
  75. }
  76. return nil;
  77. }
  78. @end

海阔天空:歌词

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注