Skip to content

语音评论 - AutomationVoice ​

AutomationVoice 给自动化任务合成并播放语音。音色、评论概率、播放音量在设置页的「语音评论」(type 为 voiceSetting)里配置。本地提示音、背景音乐用 Audio。

设置页 description 是接口路径 JSON,缺省走下面这些地址:

json
{
  "checkBind": "/dke/checkBind",
  "voiceProfiles": "/dke/voiceProfiles",
  "voiceTestAudio": "/dke/voiceTestAudio",
  "voiceUsage": "/dke/voiceUsage",
  "voiceGenerate": "/dke/voiceGenerate",
  "voiceRecords": "/dke/voiceRecords"
}

notice 会显示在语音设置页顶部。

设置页会记下当前音色和评论概率,脚本用 isReady()、getCommentProbability()、tts() 读取。外设开关和播放音量在默认存储里:

javascript
let usePeripheral = Storage.getBoolean('voice_peripheral_enabled');
let vol = Storage.exist('voice_play_volume') ? Storage.getInteger('voice_play_volume') : 80;

isReady() ​

返回: {boolean}

已登录且已选择音色时为 true。

javascript
if (!AutomationVoice.isReady()) {
    FloatDialogs.show('提示', '激活失效或者未选择语音角色');
    System.exit();
}

getCommentProbability() ​

返回: {number} 0–100。未设置或不是数字时为 0。

javascript
let rate = AutomationVoice.getCommentProbability();
if (rate < Math.random() * 100) {
    // 本次不发语音评论
}

hasAudioOutput() ​

返回: {boolean} 是否已接入外接音频设备(有线耳机、USB 耳机、蓝牙),不含内置扬声器。Android 9 以下始终为 true。

打开「转录外设」时,用它判断耳机是否已插上。

javascript
if (Storage.getBoolean('voice_peripheral_enabled') && !AutomationVoice.hasAudioOutput()) {
    FloatDialogs.show('提示', '请插入音频设备');
    System.exit();
}

tts(voiceId, text) ​

参数:

  • voiceId {string} 音色 id。空字符串时使用设置页已选音色
  • text {string} 要合成的文本

返回: {object} success、audioUrl、error。成功时 audioUrl 可直接 load。相同音色和文本会走内存缓存。

正式合成会扣配额并写记录。

javascript
let r = AutomationVoice.tts('', '你好,好久不见');
if (!r.success) {
    FloatDialogs.show('提示', '合成失败,原因:' + r.error);
    System.exit();
}

ttsPreview(voiceId) ​

参数: voiceId {string} 音色 id。为空时使用已选音色。

返回: {object} 与 tts 相同。

试听。文本由服务端预设,不扣配额,不写记录。

javascript
let r = AutomationVoice.ttsPreview('123');

setPlayToSpeaker(speaker) ​

参数: speaker {boolean} true 强制内置扬声器;false 优先有线耳机 / 蓝牙(默认)

返回: {void}

在 load 之前调用。关闭转录外设、需要麦克风录下扬声器声音时传 true。走扬声器时使用闹钟声道,避免前台应用把媒体音静掉。

javascript
let usePeripheral = Storage.getBoolean('voice_peripheral_enabled');
AutomationVoice.setPlayToSpeaker(!usePeripheral);

boostVolume(percent) ​

参数: percent {number} 0–100

返回: {void}

播放前把对应声道临时调到该百分比,并记住当前系统音量。走扬声器时改闹钟声道,走外设时改媒体声道。同时申请短暂音频焦点。

javascript
let vol = Storage.exist('voice_play_volume') ? Storage.getInteger('voice_play_volume') : 80;
AutomationVoice.boostVolume(vol);

restoreVolume() ​

返回: {void}

恢复 boostVolume 之前的系统音量,并释放音频焦点。放在 finally 里,播放结束后调用。

load(source) ​

参数: source {string} 可播放地址,一般是 tts 返回的 audioUrl

返回: {boolean} 是否载入成功

载入后音量为最大,并按 setPlayToSpeaker 选择出口。

play() ​

返回: {boolean} 是否开始播放。未 load 时为 false。

playAndStop(source) ​

参数: source {string} 保留参数。调用前需要先 load。

返回: {boolean} 是否开始播放。

播放已经载入的音频,播完后自动 stop。

isPlaying() ​

返回: {boolean} 是否正在播放。

stop() ​

返回: {void}

停止播放并释放播放器。若当前强制扬声器,会关掉扬声器开关。

getDuration() ​

返回: {number} 已载入音频的时长(毫秒)。未载入或读不到时为 -1。

完整示例 ​

javascript
function speak(text) {
    if (!AutomationVoice.isReady()) {
        FloatDialogs.show('提示', '激活失效或者未选择语音角色');
        System.exit();
    }
    let usePeripheral = Storage.getBoolean('voice_peripheral_enabled');
    if (usePeripheral && !AutomationVoice.hasAudioOutput()) {
        FloatDialogs.show('提示', '请插入音频设备');
        System.exit();
    }

    let r = AutomationVoice.tts('', text);
    if (!r.success) {
        FloatDialogs.show('提示', '合成失败,原因:' + r.error);
        System.exit();
    }

    AutomationVoice.setPlayToSpeaker(!usePeripheral);
    let vol = Storage.exist('voice_play_volume') ? Storage.getInteger('voice_play_volume') : 80;
    AutomationVoice.boostVolume(vol);
    try {
        AutomationVoice.load(r.audioUrl);
        let ms = AutomationVoice.getDuration();
        console.log('语音时长:' + ms + 'ms');
        return AutomationVoice.playAndStop(r.audioUrl);
    } finally {
        AutomationVoice.restoreVolume();
    }
}

AutoJS文档 Released under the ISC License.