var mySound:Sound = new Sound();
mySound.attachSound("tomato"); // AS2 寫法;AS3 用 new Sound(new URLRequest("..."))
mySound.start();
註:原文為 AS2 風格,下文統一按 AS3 規範說明,核心參數邏輯一致。
var panVal = -100;
var intId = setInterval(function(){
panVal += 20;
mySound.setPan(panVal);
if (panVal > 100) { mySound.stop(); clearInterval(intId); }
}, 1000);
volBtn.onPress = function(){ this.startDrag(false, left, top, right, bottom); }
onEnterFrame = function(){ mySound.setVolume(int(volInput.text)); }
var offset = 0;
playBtn.onRelease = function(){ mySound.start(offset); }
pauseBtn.onRelease = function(){ offset = mySound.position/1000; mySound.stop(); }
onEnterFrame = function(){
if (mySound.position == mySound.duration) mySound.start(0, 99); // 自然結束循環
}
var offset = 0, rev = false, fwd = false;
onEnterFrame = function(){
if (rev && mySound.position > 0) { mySound.stop(); offset -= 0.5; mySound.start(offset); }
if (fwd && mySound.position <= mySound.duration) { mySound.stop(); offset += 0.5; mySound.start(offset); }
}
revBtn.onPress = function(){ offset = mySound.position/1000; rev = true; }
revBtn.onRelease = function(){ rev = false; }
fwdBtn.onPress = function(){ offset = mySound.position/1000; fwd = true; }
fwdBtn.onRelease = function(){ fwd = false; }
var bar:MovieClip = loadBar; // 寬度代表 100%
mySound.loadSound("tomato.mp3", true); // true = 流式
onEnterFrame = function(){
var pct = int(mySound.getBytesLoaded() / mySound.getBytesTotal() * 100);
bar._xscale = pct;
if (pct >= 100) { delete onEnterFrame; mySound.start(); }
};
**原因**:子 SWF 的 `Sound` 實例未綁定正確時間軸。
**修正**:`var mySound:Sound = new Sound(this);` // 傳入 `this` (當前 MC 參考)。
**錯誤**:同一時間軸 `new Sound()` 多次,後者 `setVolume` 影響所有。
**正確**:為每個聲音創建獨立空 MovieClip 容器:
var s1:Sound = new Sound(this);
s1.attachSound("a"); s1.start();
var mc:MovieClip = createEmptyMovieClip("mc", 0);
var s2:Sound = new Sound(mc);
s2.attachSound("b"); s2.setVolume(50); s2.start();
掌握上述邏輯,AS2/AS3 通用,聲音控制不再是黑盒。