[英]How to stop sound in the middle of playing
我正在嘗試編寫一些代碼,在按下按鈕時播放聲音,但是如果按下按鈕並且正在播放聲音,則聲音會暫停並再次播放,而不是只是播放和重疊。
這就是我所擁有的
var sound:alarm = new alarm();
var isPlaying:Boolean = false;
public function Main()
{
button.addEventListener(MouseEvent.CLICK,playSound);
}
public function playSound(e:Event):void
{
if(isPlaying)sound.stop();
sound.play();
isPlaying=true;
}
乍一看似乎可行,但隨后我在輸出中看到了以下內容
TypeError:錯誤#1006:stop不是函數。
在Main / playSound()TypeError:錯誤#1006:stop不是函數。
在Main / playSound()
因此,盡管stop不是Sound類的方法,但顯然可以使用。 實施此方法的正確方法是什么? 我一直在想是否可以使用更合適的條件,因為使用此代碼,在第一次單擊按鈕后每次進入函數時都會調用sound.stop(),是否有一種方法可以讓我簽入實時是否正在播放聲音?
在您的代碼中,函數playSound(e:Event)
應該是playSound(e:MouseEvent);
同樣,您的right stop()
不是Sound
類的方法,但是您不使用Sound
類,則使用alarm
類(除非警報類擴展了Sound類)。
另一方面,我搜索了google並彈出了Flash播放/暫停聲音
import flash.media.SoundChannel;
// Make sure to import the SoundChannel class
var sc:SoundChannel = new SoundChannel();
var sound:Sound = new alarm();
var isPlaying:Boolean = false;
var pausePos:Number = 0;
public function Main()
{
button.addEventListener(MouseEvent.CLICK,playSound);
}
public function playSound(e:MouseEvent):void
{
if(isPlaying) {
pausePos = sc.position;
sc.stop();
isPlaying = false;
} else {
sc = sound.play(pausePos);
isPlaying = true;
}
}
該代碼應該可以工作,但是我尚未對其進行測試,因此,如果出現任何錯誤或未達到期望的結果,請告訴我,我會解決的。
簡短的答案...好吧,我的全部答案:)。 代替使用聲音對象,請嘗試使用SoundChannel對象。 它提供了更多選項,包括音量和平衡控制,最主要的是停止。
文檔應提供足夠的信息以供使用。 這是相對常見的。
http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/media/SoundChannel.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.