Flash ActionScript 3.0加载和播放多个声音
我写了一个loadSound函数,它处理加载和播放多个声音。当我在另一个关键帧(即未写入函数的帧)中调用此函数时,此函数工作正常。但是,当我在编写函数的同一帧上调用函数时,声音就会混淆。例如,在第一个关键帧中写入函数。如果我在第二、第三和第四个关键帧(使用不同的声音)中调用此函数,效果会很好。但是,当我在第1、第2和第3个关键帧中调用相同的函数时,一切都变得一团糟。 在第一个关键帧中,声音正常播放。当我移动到第二个关键帧时,会播放第二个声音。但是当我移回到第一个关键帧时,第二个和第一个关键帧的声音都混在一起了。当我在第二、第三和第四个关键帧中调用函数时,不会发生这种情况 以下是我的职责:Flash ActionScript 3.0加载和播放多个声音,flash,audio,actionscript,soundchannel,Flash,Audio,Actionscript,Soundchannel,我写了一个loadSound函数,它处理加载和播放多个声音。当我在另一个关键帧(即未写入函数的帧)中调用此函数时,此函数工作正常。但是,当我在编写函数的同一帧上调用函数时,声音就会混淆。例如,在第一个关键帧中写入函数。如果我在第二、第三和第四个关键帧(使用不同的声音)中调用此函数,效果会很好。但是,当我在第1、第2和第3个关键帧中调用相同的函数时,一切都变得一团糟。 在第一个关键帧中,声音正常播放。当我移动到第二个关键帧时,会播放第二个声音。但是当我移回到第一个关键帧时,第二个和第一个关键帧的声
import flash.events.Event;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import flash.errors.IOError;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.display.MovieClip;
import flash.media.SoundTransform;
stop();
var s:Sound;
var localSound:SoundChannel = new SoundChannel();
var req:URLRequest;
var loadedPct:uint;
var currentFrameArr:Array = ["One","Two","Three"];
var urlArr:Array = ["sound/m01_t01_s01.mp3","sound/m01_t01_s02.mp3","sound/page10_11.mp3"];
function loadSnd(currentFrm:String)
{
s = new Sound();
s.addEventListener(ProgressEvent.PROGRESS, onSoundProgress);
s.addEventListener(Event.COMPLETE, onSoundLoaded);
s.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
localSound.stop();
for (var i:uint=0; i<currentFrameArr.length; i++)
{
if (currentFrm == currentFrameArr[i])
{
req = new URLRequest(urlArr[i]);
break;
}
}
s.load(req);
}
function onSoundProgress(evt:ProgressEvent):void
{
loadedPct = Math.round(100 * (evt.bytesLoaded / evt.bytesTotal));
trace("The sound is " + loadedPct + " % loaded");
}
function onSoundLoaded(evt:Event):void
{
localSound = s.play();
}
function onIOError(evt:IOErrorEvent):void
{
trace("The sound could not be loaded: " + evt.text);
}
等
如果我在loadSnd中传递参数“One”,它将播放urlArray中的第一个元素,“Two”第二个元素,依此类推
但是如果我在按钮中包含soundChannel.stop(),则效果很好。尽管我希望将它嵌入函数本身,因为我正计划编写一个类文件,以便将其包含在我的项目中。下面的代码,如果包括工程刚刚好
nextBtn.addEventListener(MouseEvent.CLICK, onNextClk);
function onNextClk(evt:MouseEvent):void
{
localSound.stop();
this.nextFrame();
}
prevBtn.addEventListener(MouseEvent.CLICK, onPrevClk);
function onPrevClk(evt:MouseEvent):void
{
localSound.stop();
this.prevFrame();
}
我还在想,如果在没有鼠标单击的情况下更改帧会发生什么情况,例如,如果动画结束,新的关键帧开始,我希望在该关键帧中包含不同的声音文件。
如果函数本身包含soundChannel.stop(),则无论发生什么事件(鼠标单击按钮或动画结束),它都会停止以前的声音并播放新的声音
任何帮助都将不胜感激,因为我已经为之挠头很久了。得到了解决方案。SoundMixer.stopAll();声明起了作用。完整可行的功能粘贴如下:
Got the Solution. The SoundMixer.stopAll(); statement did the trick. The complete workable function is pasted below:
import flash.events.Event;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import flash.errors.IOError;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.display.MovieClip;
import flash.media.SoundTransform;
stop();
var s:Sound;
var localSound:SoundChannel = new SoundChannel();
var req:URLRequest;
var loadedPct:uint;
var currentFrameArr:Array = ["One","Two","Three"];
var urlArr:Array = ["sound/m01_t01_s01.mp3","sound/m01_t01_s02.mp3","sound/page10_11.mp3"];
function loadSnd(currentFrm:String)
{
s = new Sound();
s.addEventListener(ProgressEvent.PROGRESS, onSoundProgress);
s.addEventListener(Event.COMPLETE, onSoundLoaded);
s.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
SoundMixer.stopAll();
localSound.stop();
for (var i:uint=0; i<currentFrameArr.length; i++)
{
if (currentFrm == currentFrameArr[i])
{
req = new URLRequest(urlArr[i]);
break;
}
}
s.load(req);
}
function onSoundProgress(evt:ProgressEvent):void
{
loadedPct = Math.round(100 * (evt.bytesLoaded / evt.bytesTotal));
trace("The sound is " + loadedPct + " % loaded");
}
function onSoundLoaded(evt:Event):void
{
localSound = s.play();
}
function onIOError(evt:IOErrorEvent):void
{
trace("The sound could not be loaded: " + evt.text);
}
Calling the function:
loadSnd("One");
loadSnd("Two"); etc
导入flash.events.Event;
导入flash.media.Sound;
导入flash.net.URLRequest;
导入flash.events.ProgressEvent;
导入flash.events.IOErrorEvent;
导入flash.errors.IOError;
导入flash.media.SoundChannel;
导入flash.media.SoundMixer;
导入flash.events.TimerEvent;
导入flash.utils.Timer;
导入flash.display.MovieClip;
导入flash.media.SoundTransform;
停止();
var s:声音;
var localSound:SoundChannel=new SoundChannel();
var-req:URLRequest;
var加载PCT:uint;
var currentFrameArr:Array=[“一”、“二”、“三”];
var urlArr:Array=[“sound/m01_t01_s01.mp3”、“sound/m01_t01_s02.mp3”、“sound/page10_11.mp3”];
函数loadSnd(currentFrm:String)
{
s=新声音();
s、 addEventListener(ProgressEvent.PROGRESS、onSoundProgress);
s、 addEventListener(Event.COMPLETE,onSoundLoaded);
s、 addEventListener(IOErrorEvent.IO_ERROR,onIOError);
SoundMixer.stopAll();
localSound.stop();
对于(变量i:uint=0;i
Got the Solution. The SoundMixer.stopAll(); statement did the trick. The complete workable function is pasted below:
import flash.events.Event;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import flash.errors.IOError;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.display.MovieClip;
import flash.media.SoundTransform;
stop();
var s:Sound;
var localSound:SoundChannel = new SoundChannel();
var req:URLRequest;
var loadedPct:uint;
var currentFrameArr:Array = ["One","Two","Three"];
var urlArr:Array = ["sound/m01_t01_s01.mp3","sound/m01_t01_s02.mp3","sound/page10_11.mp3"];
function loadSnd(currentFrm:String)
{
s = new Sound();
s.addEventListener(ProgressEvent.PROGRESS, onSoundProgress);
s.addEventListener(Event.COMPLETE, onSoundLoaded);
s.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
SoundMixer.stopAll();
localSound.stop();
for (var i:uint=0; i<currentFrameArr.length; i++)
{
if (currentFrm == currentFrameArr[i])
{
req = new URLRequest(urlArr[i]);
break;
}
}
s.load(req);
}
function onSoundProgress(evt:ProgressEvent):void
{
loadedPct = Math.round(100 * (evt.bytesLoaded / evt.bytesTotal));
trace("The sound is " + loadedPct + " % loaded");
}
function onSoundLoaded(evt:Event):void
{
localSound = s.play();
}
function onIOError(evt:IOErrorEvent):void
{
trace("The sound could not be loaded: " + evt.text);
}
Calling the function:
loadSnd("One");
loadSnd("Two"); etc