aardio 文档

aardio 范例: 游戏音效池:启动时载入,之后一句 play 播放,多个声音可以同时响

sys.soundPool 使用指南

//游戏音效池:启动时载入,之后一句 play 播放,多个声音可以同时响
//sys.soundPool 使用指南: https://www.aardio.com/zh-cn/docs/library/sys/soundPool/
import win.ui;
/*DSG{{*/
var winform = win.form(text="sys.soundPool - 游戏音效池";right=519;bottom=239)
winform.add(
btnBgm={cls="button";text="循环播放 / 停止";left=272;top=96;right=472;bottom=136;z=4};
btnBurst={cls="button";text="连发 8 次";left=272;top=36;right=472;bottom=76;z=2};
btnDing={cls="button";text="叮";left=48;top=36;right=248;bottom=76;z=1};
btnPitch={cls="button";text="随机音高";left=48;top=96;right=248;bottom=136;z=3};
lblInfo={cls="static";left=48;top=164;right=472;bottom=212;color=0x808080;z=5}
)
/*}}*/

import sys.soundPool;

//创建一次(打开音频设备约 80 毫秒)。没有声卡时为静音模式,play 什么也不做
var sounds = sys.soundPool();

//载入系统自带的提示音:WAV 文件、MP3 等文件、PCM、av.audio 生成的音效都可以
sounds.add("ding","C:\Windows\Media\Windows Ding.wav");
sounds.add("click","C:\Windows\Media\Windows Navigation Start.wav");
sounds.add("bgm","C:\Windows\Media\Windows Proximity Connection.wav",0.6); //参数 3 是基础音量

winform.btnDing.oncommand = function(){
    sounds.play("ding");
}

//同一个声音可以重叠播放,play 只是提交请求,不会卡界面
winform.btnBurst.oncommand = function(){
    for(i=1;8){
        sounds.play("click",0.8,0.8 + i * 0.1); //音高逐次升高
        thread.delay(60);
    }
}

//重复的音效每次在 1 附近随机取音高,听起来不单调
winform.btnPitch.oncommand = function(){
    sounds.play("ding",1,0.8 + math.random() * 0.4);
}

var bgm;
winform.btnBgm.oncommand = function(){
    if(bgm && sounds.isPlaying(bgm)){
        sounds.stop(bgm);
        bgm = null;
    }
    else {
        bgm = sounds.loop("bgm");
    }
}

if(sounds.available){
    winform.lblInfo.text = "声音在系统音频线程里混音,play 约 0.03 毫秒。连续点击按钮,声音会叠在一起。";
}
else {
    winform.lblInfo.text = sounds.lastError;
}

winform.show();
win.loopMessage();

Markdown 格式