zoukankan      html  css  js  c++  java
  • 歌词解析


    #import <Foundation/Foundation.h>

    @interface JYLrcItem : NSObject

    @property (nonatomic) float time;
    @property (nonatomic) NSString *lrc;

    - (BOOL)isBiggerTimeThan:(JYLrcItem *)aItem;

    @end
    ---------------------------------------------------------------------------

    #import "JYLrcItem.h"

    @implementation JYLrcItem

    - (BOOL)isBiggerTimeThan:(JYLrcItem *)aItem
    {
        return _time > aItem.time;
    }

    - (NSString *)description
    {
        return [NSString stringWithFormat:@"%.2f %@", _time,_lrc];
    }

    @end

    ---------------------------------------------------------------------------

    #import <Foundation/Foundation.h>

    @interface JYLrcParser : NSObject

    // 歌词作者 [ar:歌词作者]
    @property NSString *author;
    // 歌词所在的唱片集 [al:这首歌所在的唱片集]
    @property NSString *albume;
    // 本lrc编辑作者 [by:本LRC文件的创建者]
    @property NSString *byEditor;
    // 歌曲标题 [ti:歌词(歌曲)的标题]
    @property NSString *title;
    // 歌曲版本 [ve:程序的版本]
    @property NSString *version;

    //把解析的歌词模型 对象地址放入数组中
    //存放的是JYLrcItem类的对象地址
    @property NSMutableArray *allLrcItems;

    // 初始化这个lrc
    - (id) initWithFile:(NSString *)file;
    // 通过second取得当前的歌词
    - (NSString *) getLrcByTime:(float)second;

    //显示歌曲信息
    - (void)showSongInfo;

    - (void)showAllInfo;

    @end

    -----------------------------------------------------------------------------------

    #import "JYLrcParser.h"
    #import "JYLrcItem.h"

    @implementation JYLrcParser

    - (id)initWithFile:(NSString *)file
    {
        if (self = [super init]) {
            _allLrcItems = [[NSMutableArray alloc] init];
            //调用方法解析歌词文件
            [self parseLrcWithFile:file];
        }
        return self;
    }

    - (void)parseLrcWithFile:(NSString *)file
    {
        //1、读取歌词文件
        NSString *lrc = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
        //2、按照' '进行切割
        NSArray *ary = [lrc componentsSeparatedByString:@" "];
        /*
         [ti:蓝莲花]
         [ar:许巍]
         [al:时光-漫步]
         [by:fanver]
         [offset:500]
         [00:00.20]蓝莲花
         
         [00:20.81]
         */
        //3、遍历数组,解析每行信息
        for (NSString *str in ary) {
            //过滤空行(哪个二哥随意敲回车)
            if ([str isEqualToString:@""]) {
                continue;
            }
            
            //获取第一个字符
            unichar ch = [str characterAtIndex:1];
            //判读该行是否为歌词信息
            if (ch>='0' && ch<='9') {
                //4-1、是歌词信息
                [self parseLrcWithTimeString:str];
            } else {
                //4-2、是歌曲信息
                [self parseLrcWithSongInformation:str];
            }
        }
        //5、将歌词按照时间进行排序
        [_allLrcItems sortUsingSelector:@selector(isBiggerTimeThan:)];
    }

    //4-1、歌词信息解析
    - (void)parseLrcWithTimeString:(NSString *)timeString
    {
        /*
         [02:11.27][01:50.22][00:21.95]穿过幽暗地岁月
         */
        //按照']'进行切割
        NSArray *ary = [timeString componentsSeparatedByString:@"]"];
        
        //[00:20.81、@""
        //过滤没有歌词的时间行
        if ([ary.lastObject isEqualToString:@""]) {
            return;
        }
        /*
         [02:11.27
         [01:50.22
         [00:21.95
         穿过幽暗地岁月
         */
        for (int i=0; i<ary.count-1; i++) {
            //[02:11.27
            int min = [[ary[i] substringWithRange:NSMakeRange(1, 2)] intValue];
            float sec = [[ary[i] substringFromIndex:4] floatValue];
            float time = min*60 + sec;
            //创建歌词对象,保存信息
            JYLrcItem *item = [[JYLrcItem alloc] init];
            item.time = time;
            item.lrc = [ary lastObject];
            //将歌词对象添加到_allLrcItems
            [_allLrcItems addObject:item];
        }
    }

    //4-2、歌曲信息解析
    - (void)parseLrcWithSongInformation:(NSString *)info
    {
        /*
         [ti:蓝莲花]
         [ar:许巍]
         [al:时光-漫步]
         [by:fanver]
         */
        NSString *item = [info substringWithRange:NSMakeRange(1, 2)];
        NSString *content = [info substringWithRange:NSMakeRange(4, info.length-5)];
        if ([item isEqualToString:@"ti"]) {
            _title = content;
        } else if ([item isEqualToString:@"al"]) {
            _albume = content;
        } else if ([item isEqualToString:@"ar"]) {
            _author = content;
        } else if ([item isEqualToString:@"by"]) {
            _byEditor = content;
        }
    }

    - (void)showAllInfo
    {
        for (JYLrcItem *item in _allLrcItems) {
            printf("%s ", [[item description] UTF8String]);
        }
    }

    - (NSString *)getLrcByTime:(float)second
    {
        /*
         1
         35891115...100
         */
    #if 0
        for (int i=0; i<_allLrcItems.count; i++) {
            if ([_allLrcItems[i] time] > second) {
                return [_allLrcItems[i-(i!=0)] lrc];
            }
        }
        return [[_allLrcItems lastObject] lrc];
    #else
        int index = (int)_allLrcItems.count - 1;
        for (int i=0; i<_allLrcItems.count; i++) {
            if ([_allLrcItems[i] time] > second) {
                index = i - (i!=0);
                break;
            }
        }
        return [_allLrcItems[index] lrc];
    #endif
    }

    - (void)showSongInfo
    {
        if (_title) {
            printf("ti:%s ", [_title UTF8String]);
        }
        if (_author) {
            printf("ar:%s ", [_author UTF8String]);
        }
        if (_albume) {
            printf("al:%s ", [_albume UTF8String]);
        }
        if (_byEditor) {
            printf("by:%s ", [_byEditor UTF8String]);
        }
    }

    @end

    --------------------------------------------------------------------------------------------


    #import <Foundation/Foundation.h>
    #import "JYLrcParser.h"

    #define PATH @"/Users/qianfeng001/Desktop/wenzhe/RAIN/十五天/day15/歌词文件/蓝莲花.lrc"

    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            
            JYLrcParser *parser = [[JYLrcParser alloc] initWithFile:PATH];
            //[parser showAllInfo];
            //NSString *str = [parser getLrcByTime:10];
            //printf("%s ", [str UTF8String]);
            
            int i = 0;
            NSString *last = nil;
            while (i < 100) {
                NSString *str = [parser getLrcByTime:i];
                if (last != str) {
                    last = str;
                    system("clear");
                    [parser showSongInfo];
                    printf("%s ", [str UTF8String]);
                }
                i++;
                sleep(1);
            }
            
        }
        return 0;
    }

  • 相关阅读:
    snmp简介
    PKI简介
    小程序收藏
    mysql数据库中同一DB内倒表指令
    核心层+汇聚层+接入层
    eng
    rmon简介
    几个常用的JavaScript字符串处理函数
    C# 特性(Attribute)学习 关于 .net中[]的解释
    C#操作Excel文件(读取Excel,写入Excel)
  • 原文地址:https://www.cnblogs.com/rainwz/p/4589042.html
Copyright © 2011-2022 走看看