123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- let topTipMsg = require("topTipMsg");
- let Global = require("Global");
- class AudioRecord {
- constructor() {
- if (CC_JSB) {
- this.phonePath = jsb.fileUtils.getWritablePath();
- } else {
- this.phonePath = "";
- }
- this.playerRecoeding = {};
- this.jsbPath = "org/cocos2dx/javascript/AudioRecord";
- this.initJSBFunc({});
- }
- refreshData() {
- this.callBackData = null;
- this.playerRecoeding = {};
- }
- initJSBFunc(data) {
- let callBackData = data;
- this.playerAudioingSprite = data.playerMap;
- window.AndroidAudioJSB = {};
- window.AndroidAudioJSB.onStartPlayer = (index) => {
- callBackData && callBackData.onStartPlayer && callBackData.onStartPlayer();
- }
- window.AndroidAudioJSB.onFinishPlayer = (index) => {
- this.delteDataByMap(index);
- this.refreshUI();
- callBackData && callBackData.onFinishPlayer && callBackData.onFinishPlayer();
- }
- window.AndroidAudioJSB.onStopPlayer = () => {
- callBackData && callBackData.onStopPlayer && callBackData.onStopPlayer();
- }
- window.AndroidAudioJSB.onFailurePlayer = () => {
- callBackData && callBackData.onFailurePlayer && callBackData.onFailurePlayer();
- }
- window.AndroidAudioJSB.onStartRecorder = () => {
- callBackData && callBackData.onStartRecorder && callBackData.onStartRecorder();
- }
- window.AndroidAudioJSB.onCanceRecorderl = () => {
- callBackData && callBackData.onCanceRecorderl && callBackData.onCanceRecorderl();
- }
- window.AndroidAudioJSB.onStopRecorder = () => {
- callBackData && callBackData.onStopRecorder && callBackData.onStopRecorder();
- }
- window.AndroidAudioJSB.onFailureRecorder = () => {
- callBackData && callBackData.onFailureRecorder && callBackData.onFailureRecorder();
- }
- }
- getAudioData(chairID) {
- let fileName = this.fileName + "";
- let fileData = jsb.fileUtils.getDataFromFile(jsb.fileUtils.getWritablePath() + "GameAudio/" + fileName);
- let arrayBuffer = new ArrayBuffer(8 + 1 + fileName.length + fileData.length);
- let dv = new DataView(arrayBuffer);
- dv.pos = 8;
- Global.SetUint8(dv, chairID);
- for (let j = 0; j < fileName.length; j += 1) {
- Global.SetUint8(dv, fileName[j]);
- }
- for (let i = 0; i < fileData.length; i += 1) {
- Global.SetUint8(dv, fileData[i]);
- }
- return dv;
- }
- saveAudioPlay(data, TchairID) {
- let dataList = {};
- let index = 0;
- let dv = new DataView(data.slice(8));
- dv.pos = 0;
- dataList.chairID = Global.GetUint8(dv);
- dataList.fileName = "";
- dataList.datas = [];
- for (let i = 0; i < 13; i += 1) {
- dataList.fileName += Global.GetUint8(dv);
- }
- if (dataList.chairID == TchairID) {
- index = this.startPlayer(dataList.fileName);
- } else {
- for (let i = 0; i < dv.byteLength - 13 - 1; i += 1) {
- dataList.datas.push(Global.GetUint8(dv));
- }
- dataList.fileDatas = new Uint8Array(dataList.datas);
- let dirPath = jsb.fileUtils.getWritablePath() + "GameAudio/";
- let filepath = dirPath + dataList.fileName;
- if (!jsb.fileUtils.isDirectoryExist(dirPath)) {
- jsb.fileUtils.createDirectory(dirpath);
- } if (jsb.fileUtils.writeDataToFile(dataList.fileDatas, filepath)) {
- index = this.startPlayer(dataList.fileName);
- }
- }
- this.addDataToMap(TchairID, index);
- this.refreshUI();
- }
- addDataToMap(TchairID, index) {
- if (!this.playerRecoeding[TchairID]) {
- this.playerRecoeding[TchairID] = [];
- }
- this.playerRecoeding[TchairID].push(index);
- this.callBackData && this.callBackData.onStartPlayer(TchairID);
- }
- delteDataByMap(index) {
- let keys = Object.keys(this.playerRecoeding);
- for (let i = 0; i < keys.length; i += 1) {
- let chairData = keys[i];
- if (this.playerRecoeding[chairData].indexOf(index) >= 0) {
- this.playerRecoeding[chairData].remove(index);
- if (this.playerRecoeding[chairData].length == 0) {
- delete this.playerRecoeding[chairData];
- }
- }
- }
- }
- getSpeakState() {
- let keys = Object.keys(this.playerRecoeding);
- let map = [];
- for (let i = 0; i < keys.length; i += 1) {
- let chairData = keys[i];
- if (chairData.length > 0) {
- map.push(chairData);
- }
- }
- return map;
- }
- refreshUI() {
- let map = this.getSpeakState();
- for (let i in this.playerAudioingSprite) {
- this.playerAudioingSprite[i].active = false;
- }
- for (let i = 0; i < map.length; i += 1) {
- let chairID = map[i];
- this.playerAudioingSprite[chairID].active = true;
- }
- }
- //开始录音
- startRecorder(fileName) {
- CC_JSB && jsb.reflection.callStaticMethod(
- this.jsbPath,
- "onStartRecorder",
- "(Ljava/lang/String;)V",
- fileName
- )
- }
- //取消录音
- cancelRecorder(fileName) {
- CC_JSB && jsb.reflection.callStaticMethod(
- this.jsbPath,
- "onCancelRecorder",
- "(Ljava/lang/String;)V",
- fileName
- )
- }
- //结束录音
- stopRecorder(fileName) {
- CC_JSB && jsb.reflection.callStaticMethod(
- this.jsbPath,
- "onStopRecorder",
- "(Ljava/lang/String;)V",
- fileName
- )
- }
- //开始播放录音
- startPlayer(fileName) {
- return CC_JSB && jsb.reflection.callStaticMethod(
- this.jsbPath,
- "onStartPlayer",
- "(Ljava/lang/String;)I",
- fileName
- )
- }
- //结束播放录音
- stopPlayer() {
- CC_JSB && jsb.reflection.callStaticMethod(
- this.jsbPath,
- "onStopPlayer",
- "()V"
- )
- }
- //删除录音缓存
- deleteRecorder() {
- CC_JSB && jsb.reflection.callStaticMethod(
- this.jsbPath,
- "onDeleteRecorder",
- "()Z"
- )
- }
- //麦克风音量
- backMicNumber() {
- return CC_JSB && jsb.reflection.callStaticMethod(
- this.jsbPath,
- "backMicNumber",
- "()I"
- )
- }
- clickRecorder(data) {
- let succCallback = data.succ;
- let failCallback = data.fail;
- let micCallback = data.mic;
- let sprite = data.sprite;
- this.fileName = Date.now();
- this.recording = true;
- this.stopRecording = false;
- this.time = 0;
- this.startRecorder(this.fileName);
- let event_underTime = () => {
- clearInterval(intervalID);
- this.cancelRecorder(this.fileName);
- }
- let event_timeEnd = () => {
- clearInterval(intervalID);
- this.stopRecorder(this.fileName);
- }
- let event_cancel = () => {
- clearInterval(intervalID);
- this.cancelRecorder(this.fileName);
- }
- let intervalID = setInterval(() => {
- this.time += 25;
- if (this.stopRecording == true) {
- event_cancel();
- } else if (this.recording == false && this.time < 1000) {
- event_underTime();
- if (failCallback) {
- failCallback();
- } else {
- topTipMsg.showTopTipMsg("说话时间太短了,翻译");
- }
- } else if (this.recording == false) {
- event_timeEnd();
- succCallback();
- } else if (this.time >= 10000) {
- event_timeEnd();
- succCallback();
- }
- let mic = this.backMicNumber();
- micCallback(mic);
- sprite.fillRange = (9000 - this.time) / 9000;
- }, 25);
- }
- overRecorder() {
- this.recording = false;
- }
- removeRecorder() {
- this.stopRecording = true;
- }
- stopAndDeleteRecorder() {
- this.stopPlayer();
- this.deleteRecorder();
- }
- static getInstance() {
- if (!AudioRecord.instance) {
- AudioRecord.instance = new AudioRecord();
- window.AudioRecord = AudioRecord.instance;
- }
- return AudioRecord.instance;
- }
- }
- module.exports = AudioRecord.getInstance();
|