四九、node.js实现歌词播放滚动

// readline动态显示歌词

const fs = require('fs');
const path = require('path');
const iconv = require('iconv-lite');//安装第三方库转换编码格式
const readline = require('readline');

var filename = path.join(__dirname, './../lyrics/晴天.lrc');

var streamReader = fs.createReadStream(filename)
  .pipe(iconv.decodeStream('gbk'));


// 利用readline读取
var rl = readline.createInterface({ input: streamReader });

var begin = new Date().getTime();
rl.on('line', (line) => {
  task(line, begin);
});

var regex = /\[(\d{2})\:(\d{2})\.(\d{2})\]\s(.+)/;
function task(line, begin) {
  var matches = regex.exec(line);
  if (matches) {
    var m = parseFloat(matches[1]);
    var s = parseFloat(matches[2]);
    var f = parseFloat(matches[3]);
    var lyric = matches[4]; // 当前行歌词不是立即执行
    // 由于下达输出任务的时刻不同
    var offset = new Date().getTime() - begin;
    setTimeout(() => {
      console.log(lyric);
    }, m * 60 * 1000 + s * 1000 + f - offset);
  } else {
    // 不是一行歌词
    console.log(line);
  }
}

你可能感兴趣的:(nodejs,node.js,编码,path,歌词播放,fs)