import GameModel from "../../ts/core/GameModel"; import AudioManager from "../../ts/gear_2.3.4/manager/AudioManager"; import MsgAlert from "../../ts/game/msg/MsgAlert"; import AudioUtil from "../../ts/core/AudioUtil"; class VoiceMgr { constructor() { this.autoplay_index = 0; this.voice_list = []; this.auto_play_list = []; this.voice_played = false; this.playing_id = -1; this.record_func = null; } voiceRecord(callback) { if (!CC_JSB) { if (this.record_func) { this.record_func(false); } return; } this.startRecord(); this.record_func = callback; } startRecord() { if (CC_JSB) { if (cc.sys.OS_ANDROID == cc.sys.os) { console.log("調用jsb") if (jsb.reflection.callStaticMethod("org/cocos2dx/lib/test", "startRecord", "()I") == 0) { MsgAlert.addMsg("未開啟錄音權限"); console.log("未開啟錄音權限") return false; } console.log("正常調用") return true; } return false; } return false; } endRecord() { if (CC_JSB) { let soundName = ''; if (cc.sys.OS_ANDROID == cc.sys.os) { soundName = jsb.reflection.callStaticMethod("org/cocos2dx/lib/test", "stopRecord", "()Ljava/lang/String;"); } if (soundName && soundName.length > 0) { cc.log(soundName); let data = jsb.fileUtils.getDataFromFile(soundName); if (data && data.length < 8192) { MsgAlert.addMsg("錄音時間過短!"); return null; } return data; } } return null; } addVoice(data) { if (CC_JSB) { let audio_data = new Uint8Array(data); cc.log(audio_data); let index = this.voice_list.length; let soundName = ""; if (cc.sys.OS_ANDROID == cc.sys.os) { soundName = jsb.reflection.callStaticMethod("org/cocos2dx/lib/test", "getPath", "(Ljava/lang/String;)Ljava/lang/String;", 'voice_' + index + '.3gp'); } jsb.fileUtils.writeDataToFile(audio_data, soundName); this.voice_list[index] = soundName; if (GameModel.autoWorldVoice == 1) { this.auto_play_list.push(index); this.autoPlay(); } return index; } return 0; } playVoice(index, callback) { if (CC_JSB) { let soundName = this.voice_list[index]; if (soundName) { var rs = jsb.reflection.callStaticMethod("org/cocos2dx/lib/test", "playVoiceByPath", "(Ljava/lang/String;)Ljava/lang/String;", soundName); if (rs == "0") { console.warn("播放失敗") } else { this.voice_played = true; this.playing_id = index; let chatpanel = cc.find('Canvas/ChatPanel'); if (chatpanel) { let logic = chatpanel.getComponent('ChatPanel'); logic.changeVoicePlayedId(index); } } var cb = () => { this.playing_id = -1; this.voice_played = false; this.autoPlay(); callback&&callback(); } cc.playVoiceCB = cb; return true; } } return false; } autoPlay() { if (this.voice_played) { return; } let index = this.auto_play_list.shift(); if (index == null) { return; } this.playVoice(index, () => { this.playing_id = -1; this.voice_played = false; this.autoPlay(); }); } getPlayingId() { return this.playing_id; } } module.exports = VoiceMgr;