Skip to content

Instantly share code, notes, and snippets.

@tana
Created July 25, 2010 10:57
Show Gist options
  • Save tana/489485 to your computer and use it in GitHub Desktop.
Save tana/489485 to your computer and use it in GitHub Desktop.
コンパイル:
haxe -main Speak -swf9 speak.swf
注意:
ローカルで動かす時は、Flashの「グローバルセキュリティ設定パネル」で、speak.swfがあるディレクトリを常に信頼するようにしておく。
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.events.Event;
import flash.events.MouseEvent;
class Speak {
static var channel : SoundChannel;
static var status : Bool = false; //再生中はtrue。再生が終わるとfalseになる。
static var lasturl = ""; //最後に再生したファイルのURL。
static var lastsound : Sound; //最後に再生したSoundオブジェクト。
static function main() {
var tf = new TextField();
tf.text = "click";
flash.Lib.current.addChild(tf);
tf.addEventListener(MouseEvent.CLICK, function(e) {
speak("hello");
});
}
static function speak(text) {
if(status) {
channel.stop();
status = false;
}
var url = "http://translate.google.com/translate_tts?tl=en&q=" + StringTools.urlEncode(text);
var sound = if(url != lasturl) new Sound(new URLRequest(url)) else lastsound;
channel = sound.play();
status = true;
channel.addEventListener(Event.SOUND_COMPLETE, complete);
lasturl = url;
lastsound = sound;
}
static function complete(e : Event) {
status = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment