actionscript3计算器声音播放
actionscript3 calculator sound playing
我正在尝试使用 as3 in air 为 android 制作一个数学应用程序。我有很多框架,里面有一些东西,比如
stop();
QuestText.text = "0+2"
Button1.LabelText.text = "6";
Button2.LabelText.text = "2";
Button3.LabelText.text = "0";
我有一些声音:
从 1.mp3 到 20.mp3 和 plus.mp3 和 minus.mp3
var DigitSound0:Sound = new Sound();
var DigitSound1:Sound = new Sound();
var DigitSound2:Sound = new Sound();
var DigitSound3:Sound = new Sound();
var DigitSound4:Sound = new Sound();
var DigitSound5:Sound = new Sound();
var DigitSound6:Sound = new Sound();
var DigitSound7:Sound = new Sound();
var DigitSound8:Sound = new Sound();
var DigitSound9:Sound = new Sound();
var DigitSoundplus:Sound = new Sound();
var DigitSoundminus:Sound = new Sound();
var DigitSoundChannel:SoundChannel = new SoundChannel();
DigitSound0.load(new URLRequest(FilePath+"speech/0.mp3"));
DigitSound1.load(new URLRequest(FilePath+"speech/1.mp3"));
DigitSound2.load(new URLRequest(FilePath+"speech/2.mp3"));
DigitSound3.load(new URLRequest(FilePath+"speech/3.mp3"));
DigitSound4.load(new URLRequest(FilePath+"speech/4.mp3"));
DigitSound5.load(new URLRequest(FilePath+"speech/5.mp3"));
DigitSound6.load(new URLRequest(FilePath+"speech/6.mp3"));
DigitSound7.load(new URLRequest(FilePath+"speech/7.mp3"));
DigitSound8.load(new URLRequest(FilePath+"speech/8.mp3"));
DigitSound9.load(new URLRequest(FilePath+"speech/9.mp3"));
DigitSoundplus.load(new URLRequest(FilePath+"speech/plus.mp3"));
DigitSoundminus.load(new URLRequest(FilePath+"speech/minus.mp3"));
我只需要例如当问题是“4+5”时,让as3播放4.mp3+plus.mp3+5.mp3
我可以在每一帧的每个 mp3 之后使用 soundcomplete 事件来完成
stop();
DigitSoundChannel.addEventListener(Event.SOUND_COMPLETE, DigitSoundComplete);
DigitSoundChannel = DigitSound4.play();
function DigitSoundComplete(event:Event):void
{
ThemeSongChannel.removeEventListener(Event.SOUND_COMPLETE, DigitSoundComplete);
DigitSoundChannel = DigitSoundplus.play();
DigitSoundChannel.addEventListener(Event.SOUND_COMPLETE, DigitSoundComplete);
DigitSoundChannel = DigitSound5.play();
ThemeSongChannel.removeEventListener(Event.SOUND_COMPLETE, DigitSoundComplete);
}
QuestText.text = "4+5"
Button1.LabelText.text = "1";
Button2.LabelText.text = "0";
Button3.LabelText.text = "4";
但是我有300多帧。有没有更简单的方法?
我不会用"easier"这个词。但是更多的算法,是的,这是可能的。得知官方 Adobe 文档有一个示例 how to play a sequence of sounds,您实际上会很兴奋。让我们根据您的需要进行调整。
首先,您需要设计一个声音缓存,以便您可以按字符值获取声音。
package
{
import flash.media.Sound;
import flash.events.Event;
public class SoundCache
{
static private var loadingPlan:int;
static private var completeHandler:Function;
static private var hash:Object = new Object;
// This method will return a Sound object by character value.
static public function find(name:String):Sound
{
return hash[name];
}
static public function startEngine(handler:Function):void
{
loadingPlan = 12;
completeHandler = handler;
for (var i:int = 0; i < 10; i++)
loadSound(i + "", i + "");
loadSound("+", "plus");
loadSound("-", "minus");
}
static private function loadSound(name:String, fileName:String):void
{
var aSound:Sound = new Sound;
hash[name] = aSound;
aSound.addEventListener(Event.COMPLETE, onLoaded);
aSound.load(FilePath + "speech/" + fileName + ".mp3");
}
static private function onLoaded(e:Event):void
{
var aSound:Sound = e.target as Sound;
aSound.removeEventListener(Event.COMPLETE, onLoaded);
loadingPlan--;
if (loadingPlan == 0)
{
if (completeHandler != null)
{
completeHandler();
completeHandler = null;
}
}
}
}
}
所以在您的应用程序开始时,您调用:
import SoundCache;
SoundCache.startEngine(onCache);
function onCache():void
{
trace("All sounds are loaded!");
}
当所有声音都加载并准备就绪后,您可以使用它们来播放声音序列。让我们设计一个序列播放器。
package
{
import SoundCache;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
public class SequencePlayer
{
// A list of active sequence players. If you don't keep
// their references while they are playing,
// Garbage Collector might go and get them.
static private var list:Array = new Array;
static public function play(sequence:String):void
{
// Sanity check.
if (!sequence)
{
trace("There's nothing to play!");
return;
}
var aSeq:SequencePlayer = new SequencePlayer;
list.push(aSeq);
aSeq.sequence = sequence;
aSeq.playNext();
}
// *** NON-STATIC PART *** //
private var sequence:String;
private var channel:SoundChannel;
private function playNext():void
{
var aChar:String;
var aSound:Sound;
// While there are still characters in the sequence,
// search for sound by the first character.
// If there's no such sound - repeat.
while (sequence)
{
// Get a sound from the cache by single character.
aChar = sequence.charAt(0);
aSound = SoundCache.find(aChar);
// Remove the first character from the sequence.
sequence = sequence.substr(1);
// Stop searching if there is a valid next sound.
if (aSound) break;
}
if (aSound)
{
// If there is a valid Sound object to play, then play it
// and subscribe for the relevant event to move to the
// next sound when this one is done playing.
channel = aSound.play();
channel.addEventListener(Event.SOUND_COMPLETE, onSound);
}
else
{
// If sequence is finished and there's no valid
// sound to play, remove this sequence player
// from the keepalive list and forget about it.
var anIndex:int = list.indexOf(this);
if (anIndex > -1) list.splice(anIndex, 1);
}
}
// Event.SOUND_COMPLETE handler.
private function onSound(e:Event):void
{
// Sanity checks.
if (!channel) return;
if (e.target != channel) return;
// Release the current SoundChannel object.
channel.removeEventListener(Event.SOUND_COMPLETE, onSound);
channel = null;
// Move on to the next sound if any.
playNext();
}
}
}
虽然到目前为止这一切看起来很可怕,但我们快完成了。此时,播放随机声音序列所需要做的就是调用 SequencePlayer.play(...) 方法。上面所有的恐怖都将你的问题归结为:
import SequencePlayer;
// It will ignore the space charaters so it's fine.
SequencePlayer.play(" 5 + 0 ");
请记住,虽然我试图为您提供有据可查且有效的解决方案,但我从未真正测试过它,所以请原谅我的随机打字错误。
我正在尝试使用 as3 in air 为 android 制作一个数学应用程序。我有很多框架,里面有一些东西,比如
stop();
QuestText.text = "0+2"
Button1.LabelText.text = "6";
Button2.LabelText.text = "2";
Button3.LabelText.text = "0";
我有一些声音: 从 1.mp3 到 20.mp3 和 plus.mp3 和 minus.mp3
var DigitSound0:Sound = new Sound();
var DigitSound1:Sound = new Sound();
var DigitSound2:Sound = new Sound();
var DigitSound3:Sound = new Sound();
var DigitSound4:Sound = new Sound();
var DigitSound5:Sound = new Sound();
var DigitSound6:Sound = new Sound();
var DigitSound7:Sound = new Sound();
var DigitSound8:Sound = new Sound();
var DigitSound9:Sound = new Sound();
var DigitSoundplus:Sound = new Sound();
var DigitSoundminus:Sound = new Sound();
var DigitSoundChannel:SoundChannel = new SoundChannel();
DigitSound0.load(new URLRequest(FilePath+"speech/0.mp3"));
DigitSound1.load(new URLRequest(FilePath+"speech/1.mp3"));
DigitSound2.load(new URLRequest(FilePath+"speech/2.mp3"));
DigitSound3.load(new URLRequest(FilePath+"speech/3.mp3"));
DigitSound4.load(new URLRequest(FilePath+"speech/4.mp3"));
DigitSound5.load(new URLRequest(FilePath+"speech/5.mp3"));
DigitSound6.load(new URLRequest(FilePath+"speech/6.mp3"));
DigitSound7.load(new URLRequest(FilePath+"speech/7.mp3"));
DigitSound8.load(new URLRequest(FilePath+"speech/8.mp3"));
DigitSound9.load(new URLRequest(FilePath+"speech/9.mp3"));
DigitSoundplus.load(new URLRequest(FilePath+"speech/plus.mp3"));
DigitSoundminus.load(new URLRequest(FilePath+"speech/minus.mp3"));
我只需要例如当问题是“4+5”时,让as3播放4.mp3+plus.mp3+5.mp3
我可以在每一帧的每个 mp3 之后使用 soundcomplete 事件来完成
stop();
DigitSoundChannel.addEventListener(Event.SOUND_COMPLETE, DigitSoundComplete);
DigitSoundChannel = DigitSound4.play();
function DigitSoundComplete(event:Event):void
{
ThemeSongChannel.removeEventListener(Event.SOUND_COMPLETE, DigitSoundComplete);
DigitSoundChannel = DigitSoundplus.play();
DigitSoundChannel.addEventListener(Event.SOUND_COMPLETE, DigitSoundComplete);
DigitSoundChannel = DigitSound5.play();
ThemeSongChannel.removeEventListener(Event.SOUND_COMPLETE, DigitSoundComplete);
}
QuestText.text = "4+5"
Button1.LabelText.text = "1";
Button2.LabelText.text = "0";
Button3.LabelText.text = "4";
但是我有300多帧。有没有更简单的方法?
我不会用"easier"这个词。但是更多的算法,是的,这是可能的。得知官方 Adobe 文档有一个示例 how to play a sequence of sounds,您实际上会很兴奋。让我们根据您的需要进行调整。
首先,您需要设计一个声音缓存,以便您可以按字符值获取声音。
package
{
import flash.media.Sound;
import flash.events.Event;
public class SoundCache
{
static private var loadingPlan:int;
static private var completeHandler:Function;
static private var hash:Object = new Object;
// This method will return a Sound object by character value.
static public function find(name:String):Sound
{
return hash[name];
}
static public function startEngine(handler:Function):void
{
loadingPlan = 12;
completeHandler = handler;
for (var i:int = 0; i < 10; i++)
loadSound(i + "", i + "");
loadSound("+", "plus");
loadSound("-", "minus");
}
static private function loadSound(name:String, fileName:String):void
{
var aSound:Sound = new Sound;
hash[name] = aSound;
aSound.addEventListener(Event.COMPLETE, onLoaded);
aSound.load(FilePath + "speech/" + fileName + ".mp3");
}
static private function onLoaded(e:Event):void
{
var aSound:Sound = e.target as Sound;
aSound.removeEventListener(Event.COMPLETE, onLoaded);
loadingPlan--;
if (loadingPlan == 0)
{
if (completeHandler != null)
{
completeHandler();
completeHandler = null;
}
}
}
}
}
所以在您的应用程序开始时,您调用:
import SoundCache;
SoundCache.startEngine(onCache);
function onCache():void
{
trace("All sounds are loaded!");
}
当所有声音都加载并准备就绪后,您可以使用它们来播放声音序列。让我们设计一个序列播放器。
package
{
import SoundCache;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
public class SequencePlayer
{
// A list of active sequence players. If you don't keep
// their references while they are playing,
// Garbage Collector might go and get them.
static private var list:Array = new Array;
static public function play(sequence:String):void
{
// Sanity check.
if (!sequence)
{
trace("There's nothing to play!");
return;
}
var aSeq:SequencePlayer = new SequencePlayer;
list.push(aSeq);
aSeq.sequence = sequence;
aSeq.playNext();
}
// *** NON-STATIC PART *** //
private var sequence:String;
private var channel:SoundChannel;
private function playNext():void
{
var aChar:String;
var aSound:Sound;
// While there are still characters in the sequence,
// search for sound by the first character.
// If there's no such sound - repeat.
while (sequence)
{
// Get a sound from the cache by single character.
aChar = sequence.charAt(0);
aSound = SoundCache.find(aChar);
// Remove the first character from the sequence.
sequence = sequence.substr(1);
// Stop searching if there is a valid next sound.
if (aSound) break;
}
if (aSound)
{
// If there is a valid Sound object to play, then play it
// and subscribe for the relevant event to move to the
// next sound when this one is done playing.
channel = aSound.play();
channel.addEventListener(Event.SOUND_COMPLETE, onSound);
}
else
{
// If sequence is finished and there's no valid
// sound to play, remove this sequence player
// from the keepalive list and forget about it.
var anIndex:int = list.indexOf(this);
if (anIndex > -1) list.splice(anIndex, 1);
}
}
// Event.SOUND_COMPLETE handler.
private function onSound(e:Event):void
{
// Sanity checks.
if (!channel) return;
if (e.target != channel) return;
// Release the current SoundChannel object.
channel.removeEventListener(Event.SOUND_COMPLETE, onSound);
channel = null;
// Move on to the next sound if any.
playNext();
}
}
}
虽然到目前为止这一切看起来很可怕,但我们快完成了。此时,播放随机声音序列所需要做的就是调用 SequencePlayer.play(...) 方法。上面所有的恐怖都将你的问题归结为:
import SequencePlayer;
// It will ignore the space charaters so it's fine.
SequencePlayer.play(" 5 + 0 ");
请记住,虽然我试图为您提供有据可查且有效的解决方案,但我从未真正测试过它,所以请原谅我的随机打字错误。