From 7586b3fe45f2930f0ef01acc6245286aef818151 Mon Sep 17 00:00:00 2001 From: oc Date: Sun, 3 Feb 2019 18:21:28 +0800 Subject: [PATCH] stopSound --- _server/blockly/MotaAction.g4 | 17 +++++++++++++++++ _server/editor_blockly.js | 1 + docs/event.md | 6 ++++++ libs/control.js | 7 +++++++ libs/core.js | 1 + libs/events.js | 16 ++++++++++++++++ 6 files changed, 48 insertions(+) diff --git a/_server/blockly/MotaAction.g4 b/_server/blockly/MotaAction.g4 index 50d1db34..355fe318 100644 --- a/_server/blockly/MotaAction.g4 +++ b/_server/blockly/MotaAction.g4 @@ -286,6 +286,7 @@ action | loadBgm_s | freeBgm_s | playSound_s + | stopSound_s | setVolume_s | win_s | lose_s @@ -1381,6 +1382,18 @@ var code = '{"type": "playSound", "name": "'+EvalString_0+'"},\n'; return code; */; +stopSound_s + : '停止所有音效' Newline + + +/* stopSound_s +tooltip : stopSound: 停止所有音效 +helpUrl : https://h5mota.com/games/template/docs/#/event?id=stopSound%ef%bc%9a%e5%81%9c%e6%ad%a2%e6%89%80%e6%9c%89%e9%9f%b3%e6%95%88 +colour : this.soundColor +var code = '{"type": "stopSound"},\n'; +return code; +*/; + setVolume_s : '设置音量' Int '渐变时间' Int? '不等待执行完毕' Bool Newline @@ -2326,6 +2339,10 @@ ActionParser.prototype.parseAction = function() { this.next = MotaActionBlocks['freeBgm_s'].xmlText([ data.name,this.next]); break + case "stopSound": + this.next = MotaActionBlocks['stopSound_s'].xmlText([ + this.next]); + break case "setVolume": this.next = MotaActionBlocks['setVolume_s'].xmlText([ data.value, data.time||0, data.async||false, this.next]); diff --git a/_server/editor_blockly.js b/_server/editor_blockly.js index f002293a..f5dc2315 100644 --- a/_server/editor_blockly.js +++ b/_server/editor_blockly.js @@ -141,6 +141,7 @@ editor_blockly = function () { MotaActionBlocks['loadBgm_s'].xmlText(), MotaActionBlocks['freeBgm_s'].xmlText(), MotaActionBlocks['playSound_s'].xmlText(), + MotaActionBlocks['stopSound_s'].xmlText(), MotaActionBlocks['setVolume_s'].xmlText(), MotaActionBlocks['callBook_s'].xmlText(), MotaActionBlocks['callSave_s'].xmlText(), diff --git a/docs/event.md b/docs/event.md index 6f43518c..62e58bc7 100644 --- a/docs/event.md +++ b/docs/event.md @@ -1306,6 +1306,12 @@ async可选,如果为true则会异步执行(即不等待当前事件执行 值得注意的是,如果是额外添加进文件的音效,则需在main.js中this.sounds里加载它。 +### stopSound:停止所有音效 + +使用`{"type": "stopSound"}`可以停止所有音效。 + +这在人物对话音效时很有用。 + ### setVolume:设置音量 使用setVolume可以设置音量大小。 diff --git a/libs/control.js b/libs/control.js index 3df0d0ac..f336d1c1 100644 --- a/libs/control.js +++ b/libs/control.js @@ -2667,6 +2667,11 @@ control.prototype.playSound = function (sound) { var source = core.musicStatus.audioContext.createBufferSource(); source.buffer = core.material.sounds[sound]; source.connect(core.musicStatus.gainNode); + source.loop = true; + var id = parseInt(Math.random()*10000000); + source.onended = function () { + delete core.musicStatus.playingSounds[id]; + } try { source.start(0); } @@ -2676,8 +2681,10 @@ control.prototype.playSound = function (sound) { } catch (ee) { main.log(ee); + return; } } + core.musicStatus.playingSounds[id] = source; } else { core.material.sounds[sound].volume = core.musicStatus.volume; diff --git a/libs/core.js b/libs/core.js index 54f01b97..a9506ef9 100644 --- a/libs/core.js +++ b/libs/core.js @@ -54,6 +54,7 @@ function core() { 'soundStatus': true, // 是否播放SE 'playingBgm': null, // 正在播放的BGM 'gainNode': null, + 'playingSounds': {}, // 正在播放的SE 'volume': 1.0, // 音量 'cachedBgms': [], // 缓存BGM内容 'cachedBgmCount': 4, // 缓存的bgm数量 diff --git a/libs/events.js b/libs/events.js index 41720d3c..509d6428 100644 --- a/libs/events.js +++ b/libs/events.js @@ -959,6 +959,22 @@ events.prototype.doAction = function() { core.freeBgm(data.name); this.doAction(); break; + case "stopSound": + for (var i in core.musicStatus.playingSounds) { + var source = core.musicStatus.playingSounds[i]; + try { + source.stop(); + } + catch (e) { + try { + source.noteOff(0); + } + catch (e) { + main.log(e); + } + } + } + break; case "setVolume": data.value = core.clamp(parseInt(data.value)/100, 0, 1); core.setFlag("__volume__", data.value);