UNPKG

olympus-r-17plugins

Version:

A plugin of Olympus for 17zuoye.

256 lines (255 loc) 9.1 kB
import * as tslib_1 from "tslib"; import Shell17, { Platform, ClientType } from "./Shell17"; import { environment } from "olympus-r/engine/env/Environment"; import { core } from "olympus-r/core/Core"; import AudioMessage from "olympus-r/engine/audio/AudioMessage"; /** * @author Raykid * @email initial_r@qq.com * @create date 2018-01-08 * @modify date 2018-01-08 * * 一起作业微信小程序外壳 */ var Shell17WXApp = /** @class */ (function (_super) { tslib_1.__extends(Shell17WXApp, _super); function Shell17WXApp() { var _this = _super.call(this) || this; _this._voiceDict = {}; /*************************** 下面是语音引擎接口 ***************************/ _this._recordDict = {}; _this.initRecord(); return _this; } Object.defineProperty(Shell17WXApp.prototype, "platform", { get: function () { return Platform.WXApp; }, enumerable: true, configurable: true }); Object.defineProperty(Shell17WXApp.prototype, "clientType", { get: function () { return ClientType.UNKNOWN; }, enumerable: true, configurable: true }); /*************************** 下面是音频接口 ***************************/ Shell17WXApp.prototype.initAudio = function () { wx.onVoicePlayEnd(function (res) { try { window["vox"] && vox.task && vox.task.playAudioProgress && vox.task.playAudioProgress(res.tempFilePath, "ended"); // 派发AudioMessage事件 core.dispatch(AudioMessage.AUDIO_PLAY_ENDED, res.tempFilePath); } catch (err) { } }); }; /** * 开始下载音频 * * @param {string} url 音频地址 * @memberof Shell17Mobile */ Shell17WXApp.prototype.loadAudio = function (url, callback) { url = environment.toCDNHostURL(url); if (this._voiceDict[url]) { try { window["vox"] && vox.task && vox.task.loadAudioProgress && vox.task.loadAudioProgress(url, "init", 0, 1); window["vox"] && vox.task && vox.task.loadAudioProgress && vox.task.loadAudioProgress(url, "loaded", 1, 1); } catch (err) { } callback && callback(this._voiceDict[url]); } else { try { window["vox"] && vox.task && vox.task.loadAudioProgress && vox.task.loadAudioProgress(url, "init", 0, 1); window["vox"] && vox.task && vox.task.loadAudioProgress && vox.task.loadAudioProgress(url, "loading", 0.5, 1); } catch (err) { } wx.downloadVoice({ url: url, success: function (res) { try { window["vox"] && vox.task && vox.task.loadAudioProgress && vox.task.loadAudioProgress(url, "loaded", 1, 1); } catch (err) { } callback && callback(res.tempFilePath); }, fail: function (res) { try { window["vox"] && vox.task && vox.task.loadAudioProgress && vox.task.loadAudioProgress(url, res.errMsg, 0, 1); } catch (err) { } } }); } }; /** * 开始播放音频 * * @param {string} url 音频地址 * @memberof Shell17Mobile */ Shell17WXApp.prototype.playAudio = function (url) { this.loadAudio(url, function (path) { wx.playVoice({ filePath: path }); // 派发AudioMessage事件 core.dispatch(AudioMessage.AUDIO_PLAY_STARTED, url); }); }; /** * 暂停播放音频 * * @param {string} url 音频地址 * @memberof Shell17Mobile */ Shell17WXApp.prototype.pauseAudio = function (url) { wx.pauseVoice(); try { window["vox"] && vox.task && vox.task.playAudioProgress && vox.task.playAudioProgress(url, "stop"); // 派发AudioMessage事件 core.dispatch(AudioMessage.AUDIO_PLAY_STOPPED, url); } catch (err) { } }; /** * 音频跳转到指定位置 * * @param {string} url 音频地址 * @param {number} time 跳转位置(毫秒) * @memberof Shell17Mobile */ Shell17WXApp.prototype.seekAudio = function (url, time) { }; /** * 停止播放音频 * * @param {string} url 音频地址 * @memberof Shell17Mobile */ Shell17WXApp.prototype.stopAudio = function (url) { wx.stopVoice(); try { window["vox"] && vox.task && vox.task.playAudioProgress && vox.task.playAudioProgress(url, "ended"); // 派发AudioMessage事件 core.dispatch(AudioMessage.AUDIO_PLAY_ENDED, url); } catch (err) { } }; Shell17WXApp.prototype.initRecord = function () { var _this = this; wx.onVoiceRecordEnd(function (res) { // 记录录音地址 _this._recordDict[_this._curRecordParams.id] = res.tempFilePath; // 开始提交文件 wx.uploadVoice({ url: "https://edu.hivoice.cn/eval/silk", filePath: res.tempFilePath, name: "voice", formData: { text: JSON.stringify({ AudioCheck: true, DisplayText: _this._curRecordParams.str, OralConf: { phone: true } }), mode: "G" }, header: { "appkey": "gcgryazu4j26vfsqawfcq3afrihwzz5mltqpghyx", "x-os": "wechat", "score-coefficient": "1.5", "session-id": _this.uuid(), "Wrap-Create-Time": "true" }, success: function (res) { try { window["vox"] && vox.task && vox.task.scoreComplete && vox.task.scoreComplete(_this._curRecordParams.id, JSON.stringify(res), ""); window["vox"] && vox.task && vox.task.onScoreComplete && vox.task.onScoreComplete(_this._curRecordParams.id, "success", 0, "", res); } catch (err) { } }, fail: function (res) { window["vox"] && vox.task && vox.task.scoreComplete && vox.task.scoreComplete(_this._curRecordParams.id, JSON.stringify(res), ""); window["vox"] && vox.task && vox.task.onScoreComplete && vox.task.onScoreComplete(_this._curRecordParams.id, "fail", 1, " ", res); } }); }); }; Shell17WXApp.prototype.uuid = function () { for (var t = [], e = "0123456789abcdef", n = 0; n < 36; n++) t[n] = e.substr(Math.floor(16 * Math.random()), 1); return t[14] = "4", t[19] = e.substr(3 & t[19] | 8, 1), t[8] = t[13] = t[18] = t[23] = "-", t.join(""); }; Shell17WXApp.prototype.startRecord = function (params) { this._curRecordParams = params; wx.startRecord({}); }; Shell17WXApp.prototype.stopRecord = function (params) { wx.stopRecord(); }; Shell17WXApp.prototype.startPlayback = function (params) { var url = this._recordDict[params.id]; if (url) this.playAudio(url); }; Shell17WXApp.prototype.stopPlayback = function (params) { var url = this._recordDict[params.id]; if (url) this.stopAudio(url); }; return Shell17WXApp; }(Shell17)); export default Shell17WXApp;