UNPKG

@aiot-toolkit/velasim

Version:

vela for sim

561 lines (449 loc) 17.5 kB
export default function(global, globalThis, window, $app_exports$){ var $app_define_wrap$ = $app_define_wrap$ || function() {} var createAppHandler = function() { return /******/ (() => { // webpackBootstrap /******/ var __webpack_modules__ = ({ /***/ "./node_modules/@aiot-toolkit/dsl-xvm/lib/loaders/script-loader.js!./node_modules/@aiot-toolkit/dsl-xvm/lib/loaders/module-loader.js!./node_modules/@aiot-toolkit/dsl-xvm/lib/loaders/manifest-loader.js?path=/home/zdying/work/vela-watch/src!./node_modules/babel-loader/lib/index.js?cwd=/home/zdying/work/vela-watch&cacheDirectory&comments=false&configFile=/home/zdying/work/vela-watch/node_modules/@aiot-toolkit/packager/babel.config.js!./node_modules/@aiot-toolkit/dsl-xvm/lib/loaders/fragment-loader.js?index=0&type=script!./src/app.ux?uxType=app": /*!*************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************!*\ !*** ./node_modules/@aiot-toolkit/dsl-xvm/lib/loaders/script-loader.js!./node_modules/@aiot-toolkit/dsl-xvm/lib/loaders/module-loader.js!./node_modules/@aiot-toolkit/dsl-xvm/lib/loaders/manifest-loader.js?path=/home/zdying/work/vela-watch/src!./node_modules/babel-loader/lib/index.js?cwd=/home/zdying/work/vela-watch&cacheDirectory&comments=false&configFile=/home/zdying/work/vela-watch/node_modules/@aiot-toolkit/packager/babel.config.js!./node_modules/@aiot-toolkit/dsl-xvm/lib/loaders/fragment-loader.js?index=0&type=script!./src/app.ux?uxType=app ***! \*************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************/ /***/ ((module, __unused_webpack_exports, __webpack_require__) => { module.exports = function __scriptModule__ (module, exports, $app_require$){"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _system = _interopRequireDefault($app_require$("@app-module/system.audio")); var _player = _interopRequireDefault(__webpack_require__(/*! ./player */ "./src/player.js")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var _default = { player: new _player.default(), onCreate() { this.$def.player.init(); }, isInLoginPage: false }; exports.default = _default;} /***/ }), /***/ "./src/manifest.json": /*!***************************!*\ !*** ./src/manifest.json ***! \***************************/ /***/ ((module) => { "use strict"; module.exports = JSON.parse('{"package":"com.application.watch.vela","name":"qqmusic","versionName":"1.0.0","versionCode":1,"minPlatformVersion":1000,"icon":"/common/logo.png","deviceTypeList":["watch"],"features":[{"name":"system.router"},{"name":"system.fetch"},{"name":"system.storage"},{"name":"system.device"},{"name":"system.audio"},{"name":"system.request"},{"name":"system.file"}],"config":{"logLevel":"log","designWidth":480},"display":{"backgroundColor":"#000000"},"router":{"entry":"pages/player","pages":{"pages/login":{"component":"index"},"pages/user":{"component":"index"},"pages/player":{"component":"index"},"pages/song-list":{"component":"index"},"pages/radio-list":{"component":"index"}}}}'); /***/ }), /***/ "./src/common/utils/files.js": /*!***********************************!*\ !*** ./src/common/utils/files.js ***! \***********************************/ /***/ ((__unused_webpack_module, exports) => { "use strict"; Object.defineProperty(exports, "__esModule", ({ value: true })); exports.default = void 0; var _system = _interopRequireDefault($app_require$("@app-module/system.file")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var _default = { writeText(path, text) { return new Promise((resolve, reject) => { let _path = path.replace(/^\/+/, ''); let uri = `internal://${_path}`; // console.log('写入文件:::', uri, typeof text, text); _system.default.writeText({ uri, text, success: function () { // console.log('file write success') resolve(); }, fail: function (data, code) { let msg = `file write fail, path = ${uri} code = ${code}`; console.log(msg); reject(Error(msg)); } }); }); }, readText(path) { return new Promise((resolve, reject) => { let _path = path.replace(/^\/+/, ''); let uri = `internal://${_path}`; _system.default.readText({ uri, success: function (data) { // console.log('file read success, text = ' + data.text); resolve(data.text); }, fail: function (data, code) { let msg = `file read fail, path = ${uri} code = ${code}`; console.log(data, msg); reject(Error(msg)); } }); }); }, delete(path) { if (!path.startsWith('internal://')) { path = 'internal://' + path.replace(/^\//, ''); } console.log('删除文件:', path); return new Promise((resolve, reject) => { _system.default.delete({ uri: path, success: function (data) { console.log('delete success: ', path); resolve(); }, fail: function (data, code) { console.log(`delete fail, code = ${code}, path = ${path}`); reject(`delete fail, code = ${code}`); } }); }); }, rmdir(path) { return new Promise((resolve, reject) => { _system.default.rmdir({ uri: 'internal://files' + path, success: function (data) { resolve(data); }, fail: function (data, code) { let msg = `file read fail, path = ${path} code = ${code}`; console.log(data, msg); reject(Error(msg)); } }); }); } }; exports.default = _default; /***/ }), /***/ "./src/conf.js": /*!*********************!*\ !*** ./src/conf.js ***! \*********************/ /***/ ((__unused_webpack_module, exports) => { "use strict"; Object.defineProperty(exports, "__esModule", ({ value: true })); exports.default = void 0; var _default = { // 用户登录信息文件路径 USERINFO_FILE: '/files/user-info.json', // 手表歌单信息文件路径 WATCH_SONGLIST_FILE: '/files/watch-songlist.json', // 最近播放列表文件路径 RECENTLY_PLAYLIST_FILE: '/files/recently-play.json', // 音频文件下载路径 // MP3_DOWNLOAD_DIR: '/files/', // Toast展示时间,单位:ms TOAST_DURATION: 3000, // 最近播放列表最大限制(歌曲数量) MAX_RECENTLY_PLAYLIST_COUNT: 50, // 登录授权接口域名 // AUTH_SERVER_HOST: 'https://staging-hlthopen.io.mi.com', AUTH_SERVER_HOST: 'https://region.hlth.io.mi.com' }; exports.default = _default; /***/ }), /***/ "./src/player.js": /*!***********************!*\ !*** ./src/player.js ***! \***********************/ /***/ ((__unused_webpack_module, exports, __webpack_require__) => { "use strict"; Object.defineProperty(exports, "__esModule", ({ value: true })); exports.default = void 0; var _system = _interopRequireDefault($app_require$("@app-module/system.audio")); var _files = _interopRequireDefault(__webpack_require__(/*! ../src/common/utils/files */ "./src/common/utils/files.js")); var _conf = _interopRequireDefault(__webpack_require__(/*! ./conf */ "./src/conf.js")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const { RECENTLY_PLAYLIST_FILE, MAX_RECENTLY_PLAYLIST_COUNT } = _conf.default; class Player { constructor() { this.progressTimer = null; this.index = -1; this.list = []; this.current = null; // this.init(); } /** * 初始化相关事件,主要用来控制自动播放下一首 */ init() { _system.default.onended = () => { console.log('[PLAYER] 音乐播放完毕'); this.next(); }; // this._startTimer(); } reset() { this.list = []; this.current = null; this.index = -1; this._stopTimer(); } /** * 播放index对应的歌曲 * @param {*} index * @returns */ play(index) { var _current; let current = null; let src = null; if (index != null) { this.index = index; this.current = this.list[index]; } else { return; } current = this.current; src = (_current = current) === null || _current === void 0 ? void 0 : _current.song_play_url; if (!src) { return; } this._addRecentlyPlay(); console.log('[PLAYER] 播放音乐:', current.song_name, src); _system.default.stop(); _system.default.currentTime = 0; _system.default.title = current.song_name || '未知'; _system.default.src = '' + src; _system.default.play(); this._startTimer(); } pause() { _system.default.pause(); this._stopTimer(); } stop() { _system.default.stop(); this._stopTimer(); } /** * 继续播放,在调用pause()后 * 需要继续播放调用这个方法 */ resume() { _system.default.play(); this._startTimer(); } prev() { return this._play(-1); } next() { return this._play(1); } /** * 向前或者向后播放下一首可播放的歌曲 * 歌曲列表中的歌曲,并非所有的都可以播放,有的没有版权或者是付费音乐,不能播放 * 这个方法会自动向前或者向后寻找下一首可播放方的歌曲 * 会循环查找:如果已经到达最后一首,会自动从开头开始查找,反之亦然。 * @param {Number} dir 寻找方向, 1:向后查找,-1: 向前查找 * @returns */ _play(dir) { let nextIndex = -1; let { list } = this; if (dir !== 1 && dir !== -1) { return '参数不正确'; } if (!Array.isArray(list) || list.length === 0) { return '播放列表为空'; } nextIndex = this.getNextPlayableIndex(dir); if (nextIndex === this.index) { // 正在播放相同的歌曲 return; } if (nextIndex === -1) { console.log('没有可以播放的音乐了...'); return '没有可播放的歌曲了'; } else { // this.index = nextIndex; // this.current = this.list[nextIndex]; this.play(nextIndex); return ''; } } /** * 记录当前播放的歌曲,存储到本地文件中 */ async _addRecentlyPlay() { try { let { song_id } = this.current; let recenlyPlaySongs = await this._getRecentlySongIds(); let index = recenlyPlaySongs.indexOf(song_id); if (index > -1) { recenlyPlaySongs.splice(index, 1); } recenlyPlaySongs.unshift(song_id); recenlyPlaySongs = recenlyPlaySongs.slice(0, MAX_RECENTLY_PLAYLIST_COUNT); // console.log('添加最近播放:', song_id, recenlyPlaySongs); await _files.default.writeText(RECENTLY_PLAYLIST_FILE, JSON.stringify(recenlyPlaySongs)); } catch (error) { console.log('最近播放存储失败:', error); } } async _getRecentlySongIds() { try { let list = (await _files.default.readText(RECENTLY_PLAYLIST_FILE)) || '[]'; return JSON.parse(list) || []; } catch (error) { console.log('最近播放列表读取失败:', error); return []; } } /** * 设置播放列表 * 要实现歌曲播放、自动播放下一首等功能 * 需要首先设置播放列表 * @param {*} list */ setPlayList(list) { // console.log('[PLAYER] 设置播放列表:', list); this.list = list || []; } /** * 获取当前的播放列表 * @returns */ getPlayList() { return this.list; } /** * 获取当前播放状态,包括正在播放的歌曲信息,时间信息 * @returns */ getPlayState() { let { current, index } = this; return { current, currentTime: _system.default.currentTime, duration: _system.default.duration, index, src: _system.default.src }; } /** * 获取下一首可播放的歌曲序号 * @param {Number} dir 获取方向,1:向后 -1:向前 * @param {Number} [from=this.index] 起始序号 * @returns */ getNextPlayableIndex(dir = 1, from = this.index) { let { list } = this; let total = list.length; let song = null; let newIndex = from + dir; let playableIndex = -1; let count = 0; if (total === 0) { return -1; } while (song == null && playableIndex == -1 && count <= total) { if (newIndex >= total) { newIndex = 0; } else if (newIndex < 0) { newIndex = total - 1; } let curr = list[newIndex]; let { playable, opi_play_flag } = curr; if (playable == 1 || opi_play_flag == 1) { song = curr; playableIndex = newIndex; } newIndex += dir; count++; } return playableIndex; } _stopTimer() { if (this.progressTimer) { clearInterval(this.progressTimer); this.progressTimer = null; } } /** * 启动定时器,定时检查播放状态 * 播放完成后自动播放下一首 * **使用audio.onended事件来播放下一首更好。这个只是个不完美的替代方案,后续可能会删除这个方法** * @returns */ _startTimer() { // this._stopTimer(); if (this.progressTimer) { return; } // this.progressTimer = setGlobalInterval(() => { // try { // console.log('[PLAYER] audio progress:', audio.currentTime, audio.duration); // if (audio.currentTime >= audio.duration - 1) { // console.log('[PLAYER] play next audio.'); // this.next(); // } // } catch (error) { // console.log('[PLAYER] audio progress error:', error); // } // }, 1000); } } exports.default = Player; /***/ }) /******/ }); /************************************************************************/ /******/ // The module cache /******/ var __webpack_module_cache__ = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ if(__webpack_module_cache__[moduleId]) { /******/ return __webpack_module_cache__[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = __webpack_module_cache__[moduleId] = { /******/ // no module.id needed /******/ // no module.loaded needed /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /************************************************************************/ var __webpack_exports__ = {}; // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk. (() => { /*!*******************************!*\ !*** ./src/app.ux?uxType=app ***! \*******************************/ var $app_script$ = __webpack_require__(/*! !../node_modules/@aiot-toolkit/dsl-xvm/lib/loaders/script-loader.js!../node_modules/@aiot-toolkit/dsl-xvm/lib/loaders/module-loader.js!../node_modules/@aiot-toolkit/dsl-xvm/lib/loaders/manifest-loader.js?path=/home/zdying/work/vela-watch/src!../node_modules/babel-loader/lib/index.js?cwd=/home/zdying/work/vela-watch&cacheDirectory&comments=false&configFile=/home/zdying/work/vela-watch/node_modules/@aiot-toolkit/packager/babel.config.js!../node_modules/@aiot-toolkit/dsl-xvm/lib/loaders/fragment-loader.js?index=0&type=script!./app.ux?uxType=app */ "./node_modules/@aiot-toolkit/dsl-xvm/lib/loaders/script-loader.js!./node_modules/@aiot-toolkit/dsl-xvm/lib/loaders/module-loader.js!./node_modules/@aiot-toolkit/dsl-xvm/lib/loaders/manifest-loader.js?path=/home/zdying/work/vela-watch/src!./node_modules/babel-loader/lib/index.js?cwd=/home/zdying/work/vela-watch&cacheDirectory&comments=false&configFile=/home/zdying/work/vela-watch/node_modules/@aiot-toolkit/packager/babel.config.js!./node_modules/@aiot-toolkit/dsl-xvm/lib/loaders/fragment-loader.js?index=0&type=script!./src/app.ux?uxType=app") $app_script$({}, $app_exports$, $app_require$) $app_exports$.default.manifest = __webpack_require__(/*! ./manifest.json */ "./src/manifest.json") })(); /******/ })() ; }; return createAppHandler(); };