not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.8 KiB

  1. /*
  2. meSpeakFallback.as
  3. An experimental fallback option for meSpeak to play wav files
  4. Wav-files are sent as a plain array of uint 8-bit data via ExternalInterface
  5. JS interface:
  6. meSpeakFallback.play( <wav-array> );
  7. meSpeak.setVolume( value ) // 0 >= value <= 1
  8. Handshake: calls JS function 'meSpeakFallbackHandshake()', when initialized and ready
  9. Norbert Landsteiner, www.masswerk.at, July 2013
  10. */
  11. package {
  12. import flash.display.Sprite;
  13. import flash.events.Event;
  14. import flash.external.ExternalInterface;
  15. import flash.media.Sound;
  16. import flash.media.SoundTransform;
  17. import flash.utils.ByteArray;
  18. import flash.utils.setTimeout;
  19. import org.as3wavsound.WavSound;
  20. import org.as3wavsound.WavSoundChannel;
  21. public class meSpeakFallback extends Sprite
  22. {
  23. private var sndTransform:SoundTransform=new SoundTransform(1, 0);
  24. public function meSpeakFallback()
  25. {
  26. initExtIF();
  27. }
  28. private function initExtIF():void {
  29. var available:Boolean=false;
  30. try {
  31. if (ExternalInterface.available) {
  32. ExternalInterface.addCallback("play", play);
  33. ExternalInterface.addCallback("setVolume", setVolume);
  34. ExternalInterface.call("meSpeakFallbackHandshake");
  35. available=true;
  36. }
  37. }
  38. catch (e:Error) {}
  39. if (!available) setTimeout(initExtIF, 100);
  40. }
  41. public function setVolume(v:Number=0):void {
  42. if (v>=0 && v<=1) sndTransform.volume=v;
  43. }
  44. public function play(data:Array=null):void {
  45. if (!data) return;
  46. var l:uint=data.length;
  47. if (!l) return;
  48. // copy data to a ByteArray (oops: time, memory!)
  49. var ba:ByteArray=new ByteArray();
  50. ba.length=l;
  51. for (var i:uint=0; i<l; i++) ba.writeByte(data[i]);
  52. // play sound with global volume
  53. var snd:WavSound = new WavSound(ba);
  54. var chnl:WavSoundChannel=snd.play(0, 0, sndTransform);
  55. }
  56. }
  57. }