lrc.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. (function webpackUniversalModuleDefinition(root, factory) {
  2. if(typeof exports === 'object' && typeof module === 'object')
  3. module.exports = factory();
  4. else if(typeof define === 'function' && define.amd)
  5. define([], factory);
  6. else if(typeof exports === 'object')
  7. exports["Lyric"] = factory();
  8. else
  9. root["Lyric"] = factory();
  10. })(this, function() {
  11. return /******/ (function(modules) { // webpackBootstrap
  12. /******/ // The module cache
  13. /******/ var installedModules = {};
  14. /******/
  15. /******/ // The require function
  16. /******/ function __webpack_require__(moduleId) {
  17. /******/
  18. /******/ // Check if module is in cache
  19. /******/ if(installedModules[moduleId])
  20. /******/ return installedModules[moduleId].exports;
  21. /******/
  22. /******/ // Create a new module (and put it into the cache)
  23. /******/ var module = installedModules[moduleId] = {
  24. /******/ i: moduleId,
  25. /******/ l: false,
  26. /******/ exports: {}
  27. /******/ };
  28. /******/
  29. /******/ // Execute the module function
  30. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  31. /******/
  32. /******/ // Flag the module as loaded
  33. /******/ module.l = true;
  34. /******/
  35. /******/ // Return the exports of the module
  36. /******/ return module.exports;
  37. /******/ }
  38. /******/
  39. /******/
  40. /******/ // expose the modules object (__webpack_modules__)
  41. /******/ __webpack_require__.m = modules;
  42. /******/
  43. /******/ // expose the module cache
  44. /******/ __webpack_require__.c = installedModules;
  45. /******/
  46. /******/ // identity function for calling harmony imports with the correct context
  47. /******/ __webpack_require__.i = function(value) { return value; };
  48. /******/
  49. /******/ // define getter function for harmony exports
  50. /******/ __webpack_require__.d = function(exports, name, getter) {
  51. /******/ if(!__webpack_require__.o(exports, name)) {
  52. /******/ Object.defineProperty(exports, name, {
  53. /******/ configurable: false,
  54. /******/ enumerable: true,
  55. /******/ get: getter
  56. /******/ });
  57. /******/ }
  58. /******/ };
  59. /******/
  60. /******/ // getDefaultExport function for compatibility with non-harmony modules
  61. /******/ __webpack_require__.n = function(module) {
  62. /******/ var getter = module && module.__esModule ?
  63. /******/ function getDefault() { return module['default']; } :
  64. /******/ function getModuleExports() { return module; };
  65. /******/ __webpack_require__.d(getter, 'a', getter);
  66. /******/ return getter;
  67. /******/ };
  68. /******/
  69. /******/ // Object.prototype.hasOwnProperty.call
  70. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  71. /******/
  72. /******/ // __webpack_public_path__
  73. /******/ __webpack_require__.p = "";
  74. /******/
  75. /******/ // Load entry module and return exports
  76. /******/ return __webpack_require__(__webpack_require__.s = 20);
  77. /******/ })
  78. /************************************************************************/
  79. /******/ ([
  80. /* 0 */
  81. /***/ (function(module, exports, __webpack_require__) {
  82. // Thank's IE8 for his funny defineProperty
  83. module.exports = !__webpack_require__(3)(function(){
  84. return Object.defineProperty({}, 'a', {get: function(){ return 7; }}).a != 7;
  85. });
  86. /***/ }),
  87. /* 1 */
  88. /***/ (function(module, exports) {
  89. module.exports = function(it){
  90. return typeof it === 'object' ? it !== null : typeof it === 'function';
  91. };
  92. /***/ }),
  93. /* 2 */
  94. /***/ (function(module, exports) {
  95. var core = module.exports = {version: '2.4.0'};
  96. if(typeof __e == 'number')__e = core; // eslint-disable-line no-undef
  97. /***/ }),
  98. /* 3 */
  99. /***/ (function(module, exports) {
  100. module.exports = function(exec){
  101. try {
  102. return !!exec();
  103. } catch(e){
  104. return true;
  105. }
  106. };
  107. /***/ }),
  108. /* 4 */
  109. /***/ (function(module, exports) {
  110. // https://github.com/zloirock/core-js/issues/86#issuecomment-115759028
  111. var global = module.exports = typeof window != 'undefined' && window.Math == Math
  112. ? window : typeof self != 'undefined' && self.Math == Math ? self : Function('return this')();
  113. if(typeof __g == 'number')__g = global; // eslint-disable-line no-undef
  114. /***/ }),
  115. /* 5 */
  116. /***/ (function(module, exports, __webpack_require__) {
  117. var anObject = __webpack_require__(11)
  118. , IE8_DOM_DEFINE = __webpack_require__(16)
  119. , toPrimitive = __webpack_require__(18)
  120. , dP = Object.defineProperty;
  121. exports.f = __webpack_require__(0) ? Object.defineProperty : function defineProperty(O, P, Attributes){
  122. anObject(O);
  123. P = toPrimitive(P, true);
  124. anObject(Attributes);
  125. if(IE8_DOM_DEFINE)try {
  126. return dP(O, P, Attributes);
  127. } catch(e){ /* empty */ }
  128. if('get' in Attributes || 'set' in Attributes)throw TypeError('Accessors not supported!');
  129. if('value' in Attributes)O[P] = Attributes.value;
  130. return O;
  131. };
  132. /***/ }),
  133. /* 6 */
  134. /***/ (function(module, exports, __webpack_require__) {
  135. "use strict";
  136. exports.__esModule = true;
  137. exports.default = function (instance, Constructor) {
  138. if (!(instance instanceof Constructor)) {
  139. throw new TypeError("Cannot call a class as a function");
  140. }
  141. };
  142. /***/ }),
  143. /* 7 */
  144. /***/ (function(module, exports, __webpack_require__) {
  145. "use strict";
  146. exports.__esModule = true;
  147. var _defineProperty = __webpack_require__(8);
  148. var _defineProperty2 = _interopRequireDefault(_defineProperty);
  149. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  150. exports.default = function () {
  151. function defineProperties(target, props) {
  152. for (var i = 0; i < props.length; i++) {
  153. var descriptor = props[i];
  154. descriptor.enumerable = descriptor.enumerable || false;
  155. descriptor.configurable = true;
  156. if ("value" in descriptor) descriptor.writable = true;
  157. (0, _defineProperty2.default)(target, descriptor.key, descriptor);
  158. }
  159. }
  160. return function (Constructor, protoProps, staticProps) {
  161. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  162. if (staticProps) defineProperties(Constructor, staticProps);
  163. return Constructor;
  164. };
  165. }();
  166. /***/ }),
  167. /* 8 */
  168. /***/ (function(module, exports, __webpack_require__) {
  169. module.exports = { "default": __webpack_require__(9), __esModule: true };
  170. /***/ }),
  171. /* 9 */
  172. /***/ (function(module, exports, __webpack_require__) {
  173. __webpack_require__(19);
  174. var $Object = __webpack_require__(2).Object;
  175. module.exports = function defineProperty(it, key, desc){
  176. return $Object.defineProperty(it, key, desc);
  177. };
  178. /***/ }),
  179. /* 10 */
  180. /***/ (function(module, exports) {
  181. module.exports = function(it){
  182. if(typeof it != 'function')throw TypeError(it + ' is not a function!');
  183. return it;
  184. };
  185. /***/ }),
  186. /* 11 */
  187. /***/ (function(module, exports, __webpack_require__) {
  188. var isObject = __webpack_require__(1);
  189. module.exports = function(it){
  190. if(!isObject(it))throw TypeError(it + ' is not an object!');
  191. return it;
  192. };
  193. /***/ }),
  194. /* 12 */
  195. /***/ (function(module, exports, __webpack_require__) {
  196. // optional / simple context binding
  197. var aFunction = __webpack_require__(10);
  198. module.exports = function(fn, that, length){
  199. aFunction(fn);
  200. if(that === undefined)return fn;
  201. switch(length){
  202. case 1: return function(a){
  203. return fn.call(that, a);
  204. };
  205. case 2: return function(a, b){
  206. return fn.call(that, a, b);
  207. };
  208. case 3: return function(a, b, c){
  209. return fn.call(that, a, b, c);
  210. };
  211. }
  212. return function(/* ...args */){
  213. return fn.apply(that, arguments);
  214. };
  215. };
  216. /***/ }),
  217. /* 13 */
  218. /***/ (function(module, exports, __webpack_require__) {
  219. var isObject = __webpack_require__(1)
  220. , document = __webpack_require__(4).document
  221. // in old IE typeof document.createElement is 'object'
  222. , is = isObject(document) && isObject(document.createElement);
  223. module.exports = function(it){
  224. return is ? document.createElement(it) : {};
  225. };
  226. /***/ }),
  227. /* 14 */
  228. /***/ (function(module, exports, __webpack_require__) {
  229. var global = __webpack_require__(4)
  230. , core = __webpack_require__(2)
  231. , ctx = __webpack_require__(12)
  232. , hide = __webpack_require__(15)
  233. , PROTOTYPE = 'prototype';
  234. var $export = function(type, name, source){
  235. var IS_FORCED = type & $export.F
  236. , IS_GLOBAL = type & $export.G
  237. , IS_STATIC = type & $export.S
  238. , IS_PROTO = type & $export.P
  239. , IS_BIND = type & $export.B
  240. , IS_WRAP = type & $export.W
  241. , exports = IS_GLOBAL ? core : core[name] || (core[name] = {})
  242. , expProto = exports[PROTOTYPE]
  243. , target = IS_GLOBAL ? global : IS_STATIC ? global[name] : (global[name] || {})[PROTOTYPE]
  244. , key, own, out;
  245. if(IS_GLOBAL)source = name;
  246. for(key in source){
  247. // contains in native
  248. own = !IS_FORCED && target && target[key] !== undefined;
  249. if(own && key in exports)continue;
  250. // export native or passed
  251. out = own ? target[key] : source[key];
  252. // prevent global pollution for namespaces
  253. exports[key] = IS_GLOBAL && typeof target[key] != 'function' ? source[key]
  254. // bind timers to global for call from export context
  255. : IS_BIND && own ? ctx(out, global)
  256. // wrap global constructors for prevent change them in library
  257. : IS_WRAP && target[key] == out ? (function(C){
  258. var F = function(a, b, c){
  259. if(this instanceof C){
  260. switch(arguments.length){
  261. case 0: return new C;
  262. case 1: return new C(a);
  263. case 2: return new C(a, b);
  264. } return new C(a, b, c);
  265. } return C.apply(this, arguments);
  266. };
  267. F[PROTOTYPE] = C[PROTOTYPE];
  268. return F;
  269. // make static versions for prototype methods
  270. })(out) : IS_PROTO && typeof out == 'function' ? ctx(Function.call, out) : out;
  271. // export proto methods to core.%CONSTRUCTOR%.methods.%NAME%
  272. if(IS_PROTO){
  273. (exports.virtual || (exports.virtual = {}))[key] = out;
  274. // export proto methods to core.%CONSTRUCTOR%.prototype.%NAME%
  275. if(type & $export.R && expProto && !expProto[key])hide(expProto, key, out);
  276. }
  277. }
  278. };
  279. // type bitmap
  280. $export.F = 1; // forced
  281. $export.G = 2; // global
  282. $export.S = 4; // static
  283. $export.P = 8; // proto
  284. $export.B = 16; // bind
  285. $export.W = 32; // wrap
  286. $export.U = 64; // safe
  287. $export.R = 128; // real proto method for `library`
  288. module.exports = $export;
  289. /***/ }),
  290. /* 15 */
  291. /***/ (function(module, exports, __webpack_require__) {
  292. var dP = __webpack_require__(5)
  293. , createDesc = __webpack_require__(17);
  294. module.exports = __webpack_require__(0) ? function(object, key, value){
  295. return dP.f(object, key, createDesc(1, value));
  296. } : function(object, key, value){
  297. object[key] = value;
  298. return object;
  299. };
  300. /***/ }),
  301. /* 16 */
  302. /***/ (function(module, exports, __webpack_require__) {
  303. module.exports = !__webpack_require__(0) && !__webpack_require__(3)(function(){
  304. return Object.defineProperty(__webpack_require__(13)('div'), 'a', {get: function(){ return 7; }}).a != 7;
  305. });
  306. /***/ }),
  307. /* 17 */
  308. /***/ (function(module, exports) {
  309. module.exports = function(bitmap, value){
  310. return {
  311. enumerable : !(bitmap & 1),
  312. configurable: !(bitmap & 2),
  313. writable : !(bitmap & 4),
  314. value : value
  315. };
  316. };
  317. /***/ }),
  318. /* 18 */
  319. /***/ (function(module, exports, __webpack_require__) {
  320. // 7.1.1 ToPrimitive(input [, PreferredType])
  321. var isObject = __webpack_require__(1);
  322. // instead of the ES6 spec version, we didn't implement @@toPrimitive case
  323. // and the second argument - flag - preferred type is a string
  324. module.exports = function(it, S){
  325. if(!isObject(it))return it;
  326. var fn, val;
  327. if(S && typeof (fn = it.toString) == 'function' && !isObject(val = fn.call(it)))return val;
  328. if(typeof (fn = it.valueOf) == 'function' && !isObject(val = fn.call(it)))return val;
  329. if(!S && typeof (fn = it.toString) == 'function' && !isObject(val = fn.call(it)))return val;
  330. throw TypeError("Can't convert object to primitive value");
  331. };
  332. /***/ }),
  333. /* 19 */
  334. /***/ (function(module, exports, __webpack_require__) {
  335. var $export = __webpack_require__(14);
  336. // 19.1.2.4 / 15.2.3.6 Object.defineProperty(O, P, Attributes)
  337. $export($export.S + $export.F * !__webpack_require__(0), 'Object', {defineProperty: __webpack_require__(5).f});
  338. /***/ }),
  339. /* 20 */
  340. /***/ (function(module, exports, __webpack_require__) {
  341. var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;(function (global, factory) {
  342. if (true) {
  343. !(__WEBPACK_AMD_DEFINE_ARRAY__ = [module, exports, __webpack_require__(6), __webpack_require__(7)], __WEBPACK_AMD_DEFINE_FACTORY__ = (factory),
  344. __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ?
  345. (__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__),
  346. __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
  347. } else if (typeof exports !== "undefined") {
  348. factory(module, exports, require('babel-runtime/helpers/classCallCheck'), require('babel-runtime/helpers/createClass'));
  349. } else {
  350. var mod = {
  351. exports: {}
  352. };
  353. factory(mod, mod.exports, global.classCallCheck, global.createClass);
  354. global.index = mod.exports;
  355. }
  356. })(this, function (module, exports, _classCallCheck2, _createClass2) {
  357. 'use strict';
  358. Object.defineProperty(exports, "__esModule", {
  359. value: true
  360. });
  361. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  362. var _createClass3 = _interopRequireDefault(_createClass2);
  363. function _interopRequireDefault(obj) {
  364. return obj && obj.__esModule ? obj : {
  365. default: obj
  366. };
  367. }
  368. var timeExp = /\[(\d{2,}):(\d{2})(?:\.(\d{2,3}))?]/g;
  369. var STATE_PAUSE = 0;
  370. var STATE_PLAYING = 1;
  371. var tagRegMap = {
  372. title: 'ti',
  373. artist: 'ar',
  374. album: 'al',
  375. offset: 'offset',
  376. by: 'by'
  377. };
  378. function noop() {}
  379. var Lyric = function () {
  380. function Lyric(lrc) {
  381. var hanlder = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop;
  382. (0, _classCallCheck3.default)(this, Lyric);
  383. this.lrc = lrc;
  384. this.tags = {};
  385. this.lines = [];
  386. this.handler = hanlder;
  387. this.state = STATE_PAUSE;
  388. this.curLine = 0;
  389. this._init();
  390. }
  391. (0, _createClass3.default)(Lyric, [{
  392. key: '_init',
  393. value: function _init() {
  394. this._initTag();
  395. this._initLines();
  396. }
  397. }, {
  398. key: '_initTag',
  399. value: function _initTag() {
  400. for (var tag in tagRegMap) {
  401. var matches = this.lrc.match(new RegExp('\\[' + tagRegMap[tag] + ':([^\\]]*)]', 'i'));
  402. this.tags[tag] = matches && matches[1] || '';
  403. }
  404. }
  405. }, {
  406. key: '_initLines',
  407. value: function _initLines() {
  408. var lines = this.lrc.split('\n');
  409. for (var i = 0; i < lines.length; i++) {
  410. var line = lines[i];
  411. var result = timeExp.exec(line);
  412. if (result) {
  413. var txt = line.replace(timeExp, '').trim();
  414. if (txt) {
  415. this.lines.push({
  416. time: result[1] * 60 * 1000 + result[2] * 1000 + (result[3] || 0) * 10,
  417. txt: txt
  418. });
  419. }
  420. }
  421. }
  422. this.lines.sort(function (a, b) {
  423. return a.time - b.time;
  424. });
  425. }
  426. }, {
  427. key: '_findCurNum',
  428. value: function _findCurNum(time) {
  429. for (var i = 0; i < this.lines.length; i++) {
  430. if (time <= this.lines[i].time) {
  431. return i;
  432. }
  433. }
  434. return this.lines.length - 1;
  435. }
  436. }, {
  437. key: '_callHandler',
  438. value: function _callHandler(i) {
  439. if (i < 0) {
  440. return;
  441. }
  442. this.handler({
  443. txt: this.lines[i].txt,
  444. lineNum: i
  445. });
  446. }
  447. }, {
  448. key: '_playRest',
  449. value: function _playRest() {
  450. var _this = this;
  451. var line = this.lines[this.curNum];
  452. var delay = line.time - (+new Date() - this.startStamp);
  453. this.timer = setTimeout(function () {
  454. _this._callHandler(_this.curNum++);
  455. if (_this.curNum < _this.lines.length && _this.state === STATE_PLAYING) {
  456. _this._playRest();
  457. }
  458. }, delay);
  459. }
  460. }, {
  461. key: 'play',
  462. value: function play() {
  463. var startTime = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  464. var skipLast = arguments[1];
  465. if (!this.lines.length) {
  466. return;
  467. }
  468. this.state = STATE_PLAYING;
  469. this.curNum = this._findCurNum(startTime);
  470. this.startStamp = +new Date() - startTime;
  471. if (!skipLast) {
  472. this._callHandler(this.curNum - 1);
  473. }
  474. if (this.curNum < this.lines.length) {
  475. clearTimeout(this.timer);
  476. this._playRest();
  477. }
  478. }
  479. }, {
  480. key: 'togglePlay',
  481. value: function togglePlay() {
  482. var now = +new Date();
  483. if (this.state === STATE_PLAYING) {
  484. this.stop();
  485. this.pauseStamp = now;
  486. } else {
  487. this.state = STATE_PLAYING;
  488. this.play((this.pauseStamp || now) - (this.startStamp || now), true);
  489. this.pauseStamp = 0;
  490. }
  491. }
  492. }, {
  493. key: 'stop',
  494. value: function stop() {
  495. this.state = STATE_PAUSE;
  496. clearTimeout(this.timer);
  497. }
  498. }, {
  499. key: 'seek',
  500. value: function seek(offset) {
  501. this.play(offset);
  502. }
  503. }]);
  504. return Lyric;
  505. }();
  506. exports.default = Lyric;
  507. module.exports = exports['default'];
  508. });
  509. /***/ })
  510. /******/ ]);
  511. });
  512. $(function() {
  513. var audio = document.getElementById("audio");
  514. if (!audio) {
  515. return;
  516. }
  517. var lyricObj;
  518. var initLrc=function (lyric) {
  519. var arrLyric = new Array(); //放存汉字的歌词
  520. var arrTime = new Array(); //存放时间
  521. var currentLine = 0; //当前活动的歌词行号
  522. // 开始解析歌词
  523. //将歌词解析成数组
  524. var arrly = lyric.split('\n');
  525. for (var i = 0; i < arrly.length; i++) {
  526. var str = arrly[i];
  527. str = str.substring(str.indexOf("[") + 1, str.indexOf("]"));
  528. if (str.indexOf('ti:') > -1 || str.indexOf('ar:') > -1 || str.indexOf('al:') > -1 || str.indexOf('by:') > -1) {
  529. // 歌曲特征字段
  530. var tmp = str.substring(str.indexOf(':') + 1);
  531. var text = tmp;
  532. //tag=tag.replace(/([ti|ar|al|by])/g,'');
  533. //arrLyric.push(tag+text);
  534. //arrTime.push(toSeconds('00:00:00')); //
  535. } else {
  536. var text = arrly[i].substring(arrly[i].indexOf("]") + 1);
  537. //if(text==''){text='&nbsp;';}
  538. arrLyric.push(text); //放歌词
  539. arrTime.push(toSeconds(str)); //放时间
  540. }
  541. }
  542. // 所有歌词按时间顺序排列
  543. for (var k = 0; k < arrTime.length; k++) {
  544. for (var j = 0; j < arrTime.length - k; j++) {
  545. if (arrTime[j] > arrTime[j + 1]) {
  546. temp = arrTime[j];
  547. arrTime[j] = arrTime[j + 1];
  548. arrTime[j + 1] = temp;
  549. temp = arrLyric[j];
  550. arrLyric[j] = arrLyric[j + 1];
  551. arrLyric[j + 1] = temp;
  552. }
  553. }
  554. }
  555. var lrc2textArr=document.querySelectorAll("lrc2text");
  556. for(var i = 0; i < lrc2textArr.length; i++){
  557. var startLine=lrc2textArr[i].attributes.startLine?parseInt(lrc2textArr[i].attributes.startLine.value):1;
  558. var endLine=lrc2textArr[i].attributes.endLine?parseInt(lrc2textArr[i].attributes.endLine.value):arrLyric.length;
  559. var lcHtml="";
  560. for (var k = startLine-1; k <= endLine-1; k++) {
  561. var lc = arrLyric[k].split("<br/>");
  562. lcHtml+="<tai>"+lc[0]+"</tai><roman>"+lc[1]+"</roman><zh>"+lc[2]+"</zh>";
  563. }
  564. lrc2textArr[i].innerHTML=lcHtml;
  565. }
  566. // 显示歌词
  567. var displayLine = function(lineID,num){
  568. var lc = arrLyric[num].split("<br/>");
  569. $(lineID+" span:eq(0)").text(lc[0]);
  570. if (lc.length > 1) {
  571. $(lineID+" span:eq(1)").text(lc[1]);
  572. }else{
  573. $(lineID+" span:eq(1)").text("");
  574. }
  575. if (lc.length > 2) {
  576. $(lineID+" span:eq(2)").text(lc[2]);
  577. }else{
  578. $(lineID+" span:eq(2)").text("");
  579. }
  580. };
  581. var handler = function ({lineNum, txt}){
  582. if(currentLine==lineNum){
  583. return;
  584. }
  585. if (lineNum > 0) {
  586. displayLine("#line-last",lineNum-1);
  587. }
  588. if (lineNum < arrTime.length - 1) {
  589. displayLine("#line-next",lineNum+1);
  590. }
  591. displayLine("#line-current",lineNum);
  592. currentLine = lineNum;
  593. }
  594. if(lyricObj){
  595. lyricObj.stop();
  596. }
  597. lyricObj = new Lyric(lyric, handler);
  598. var bugInterval=undefined;
  599. audio.onplay=function(){
  600. if(bugInterval){return;}
  601. bugInterval=setInterval(function(){
  602. audio.currentTime=audio.currentTime-0.001;
  603. console.log("自动倒退:"+audio.currentTime);
  604. },5*60*1000);
  605. }
  606. audio.onpause=function(){
  607. lyricObj.stop();
  608. clearInterval(bugInterval);
  609. bugInterval=undefined;
  610. }
  611. audio.ontimeupdate=function(){
  612. var ctime=audio.currentTime;
  613. if(ctime==0){//lyricObj.seek(0)不会调用handler显示歌词,手动调用一次
  614. handler({lineNum:1,txt:""});
  615. }
  616. lyricObj.seek(ctime * 1000);
  617. lyricObj.stop();
  618. };
  619. }
  620. var toSeconds=function (t) { //把形如:01:25的时间转化成秒;
  621. var m = t.substring(0, t.indexOf(":"));
  622. var s = t.substring(t.indexOf(":") + 1);
  623. s = parseFloat(s.replace(/\b(0+)/gi, ""));
  624. if (isNaN(s)) s = 0;
  625. var totalt = parseInt(m) * 60 + s;
  626. if (isNaN(totalt)) return 0;
  627. return totalt;
  628. }
  629. var $container = $('#lyriccontainer');
  630. var loadLrc=function(){
  631. var url = audio.src;
  632. var urlArr = url.split('?');
  633. var k = urlArr[0],
  634. appU = k.split('/');
  635. var srcFileExt = appU[appU.length - 1].split('.')[1];
  636. url = url.replace("." + srcFileExt, ".lrc");
  637. var url = "../lrc/"+appU[appU.length - 1].replace("." + srcFileExt, ".lrc");
  638. $.get(url,
  639. function(data) {
  640. var lrcContent = data;
  641. if (!lrcContent || lrcContent == "") {
  642. return;
  643. }
  644. initLrc(lrcContent);
  645. });
  646. };
  647. var list=new Array();
  648. var playIndex=0;
  649. var doPlay=function(){
  650. if(playIndex>list.length-1||playIndex<0){
  651. return;
  652. }
  653. audio.src = list[playIndex];
  654. loadLrc();
  655. if(playIndex<list.length-1){
  656. $("#playNext").prop("title",list[playIndex+1]);
  657. audio.onended=playEndedHandler;
  658. }else{
  659. $("#playNext").prop("title","没有了");
  660. }
  661. if(playIndex>0){
  662. $("#playPrev").prop("title",list[playIndex-1]);
  663. }else{
  664. $("#playPrev").prop("title","没有了");
  665. }
  666. audio.play();
  667. };
  668. if ($("#audio sourceList").length>0) {
  669. list=$("#audio sourceList").attr("src").split(",");
  670. audio.src=list[0];
  671. loadLrc();
  672. if(list.length>1){
  673. var playEndedHandler=function (){
  674. playIndex++;
  675. doPlay();
  676. };
  677. $("#playNext").prop("title",list[1]);
  678. audio.onended=playEndedHandler;
  679. }
  680. }
  681. audio.onerror = audio.onabort = audio.onstalled = function(m) {
  682. layer.msg('播放器发生错误:'+m.type,{
  683. type: 1,
  684. title: false,
  685. shade:0,
  686. offset:'b',
  687. time:8000
  688. });
  689. };
  690. var noSleep = new NoSleep();
  691. var enableNoSleep = false;
  692. var isFullScreen = false;
  693. var layerIndex=0;
  694. var btn_lyricFullscreen = document.getElementById("lyricFullscreen");
  695. // 监听全屏事件触发
  696. var fullScreenChangeListener = function() {
  697. isFullScreen = !!(
  698. document.fullscreen ||
  699. document.mozFullScreen ||
  700. document.webkitIsFullScreen ||
  701. document.webkitFullScreen ||
  702. document.msFullScreen
  703. );
  704. doChangeFullScreen(isFullScreen);
  705. };
  706. var doChangeFullScreen=function(isFullScreen){
  707. if (isFullScreen) {
  708. if(!enableNoSleep){noSleep.enable();}
  709. $(btn_lyricFullscreen).html('退出全屏');
  710. enableNoSleep = true;
  711. $container.removeClass("def-lyric").addClass("fullscreen-lyric");
  712. } else {
  713. if(enableNoSleep){noSleep.disable();}
  714. $(btn_lyricFullscreen).html('全屏播放');
  715. enableNoSleep = false;
  716. $container.removeClass("fullscreen-lyric").addClass("def-lyric");
  717. }
  718. $("#lyricSleep").prop("checked",enableNoSleep);
  719. };
  720. if (btn_lyricFullscreen) {
  721. btn_lyricFullscreen.addEventListener('click',
  722. function(event) {
  723. var element = $container[0];
  724. audio.play();
  725. // 判断浏览器前缀
  726. if (document.fullscreenEnabled) {
  727. if($(btn_lyricFullscreen).html()=='全屏播放'){
  728. if (element.requestFullscreen) {
  729. element.requestFullscreen();
  730. } else if (element.mozRequestFullScreen) {
  731. element.mozRequestFullScreen();
  732. } else if (element.webkitRequestFullscreen) {
  733. element.webkitRequestFullscreen();
  734. } else if (element.msRequestFullscreen) {
  735. element.msRequestFullscreen();
  736. }
  737. }else{
  738. document.exitFullscreen();
  739. }
  740. }else{
  741. if($(btn_lyricFullscreen).html()=='全屏播放'){
  742. layerIndex=layer.open({
  743. type: 1,
  744. title: false,
  745. content: $container,
  746. end : function() {
  747. isFullScreen=false;
  748. doChangeFullScreen(false);
  749. }
  750. });
  751. isFullScreen=true;
  752. doChangeFullScreen(true);
  753. layer.full(layerIndex);
  754. }else{
  755. isFullScreen=false;
  756. doChangeFullScreen(false);
  757. layer.close(layerIndex);
  758. }
  759. }
  760. });
  761. }
  762. ['fullscreenchange','webkitfullscreenchange','mozfullscreenchange'].forEach((item,index) => {
  763. $container[0].addEventListener(item, () => fullScreenChangeListener());
  764. });
  765. var btn_lyricBigger = document.getElementById("lyricBigger");
  766. //调节字体大小
  767. var adjustFontSize = function (sizeOffset){
  768. var size='',num=0;
  769. if(isFullScreen){
  770. var styleStr=($('#lyriccontainer').attr("style")+"");
  771. var styleArr=$.map(styleStr.split(";"),function(node){
  772. var returnNode=node;
  773. if(node.indexOf("--full-font-size")>-1){
  774. num = $.trim(node.split(":")[1]);
  775. var unit = "vmin";
  776. num = parseFloat(num.substring(0,num.indexOf(unit)));
  777. num += sizeOffset;
  778. size = num + unit;
  779. returnNode="--full-font-size:"+size;
  780. }
  781. return returnNode;
  782. });
  783. $('#lyriccontainer').attr("style",styleArr.join(";"));
  784. }else{
  785. var styleStr=($('#lyriccontainer').attr("style")+"");
  786. var styleArr=$.map(styleStr.split(";"),function(node){
  787. var returnNode=node;
  788. if(node.indexOf("--default-font-size")>-1){
  789. num = $.trim(node.split(":")[1]);
  790. var unit = "rem";
  791. num = parseFloat(num.substring(0,num.indexOf(unit)));
  792. num += sizeOffset;
  793. size = num + unit;
  794. returnNode="--default-font-size:"+size;
  795. }
  796. return returnNode;
  797. });
  798. $('#lyriccontainer').attr("style",styleArr.join(";"));
  799. }
  800. };
  801. if (btn_lyricBigger) {
  802. btn_lyricBigger.addEventListener('click',
  803. function() {
  804. adjustFontSize(1);
  805. },
  806. false);
  807. }
  808. var btn_lyricSmaller = document.getElementById("lyricSmaller");
  809. if (btn_lyricSmaller) {
  810. btn_lyricSmaller.addEventListener('click',
  811. function() {
  812. adjustFontSize(-1);
  813. },
  814. false);
  815. }
  816. var btn_playNext = document.getElementById("playNext");
  817. if (btn_playNext) {
  818. btn_playNext.addEventListener('click',
  819. function() {
  820. playIndex++;
  821. doPlay();
  822. },
  823. false);
  824. }
  825. var btn_playPrev = document.getElementById("playPrev");
  826. if (btn_playPrev) {
  827. btn_playPrev.addEventListener('click',
  828. function() {
  829. playIndex--;
  830. doPlay();
  831. },
  832. false);
  833. }
  834. var chk_lyricSleep = document.getElementById("lyricSleep");
  835. if (chk_lyricSleep) {
  836. chk_lyricSleep.addEventListener('change',
  837. function(event) {
  838. let isCheck = event.srcElement.checked;
  839. if (isCheck) {
  840. if(!enableNoSleep){noSleep.enable();}
  841. enableNoSleep = true;
  842. } else {
  843. if(enableNoSleep){noSleep.disable();}
  844. enableNoSleep = false;
  845. }
  846. });
  847. }
  848. });