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.

227 lines
5.6 KiB

  1. /*
  2. A fallback to flash for wav-output (for IE 10)
  3. Please mind that wav data has to be copied to an ArrayBuffer object internally,
  4. since we may not send binary data to the swf.
  5. This may take some time and memory for longer utterances.
  6. */
  7. var meSpeakFlashFallback = new function() {
  8. var swfDefaultId='meSpeakFallback',
  9. swfDefaultUrl='meSpeakFallback.swf',
  10. swfElementId='', swfViaAX=false, swfInstalled=false, swfHasLoaded=false, swfVol=1;
  11. // public
  12. function swfInstallFallback(swfUrl, swfId, parentElementOrId) {
  13. var parentEl, url;
  14. if (swfInstalled) return true;
  15. if (!swfIsAvailable(10)) return false;
  16. swfInstalled=true;
  17. // set defaults
  18. swfElementId = (swfId && typeof swfId == 'string')? swfId:swfDefaultId;
  19. url = (swfUrl && typeof swfUrl == 'string')? swfUrl:swfDefaultUrl;
  20. if (parentElementOrId) {
  21. if (typeof parentElementOrId == 'string') {
  22. parentEl=document.getElementById(parentElementOrId);
  23. }
  24. else if (typeof parentElementOrId == 'object') {
  25. parentEl=parentElementOrId=null;
  26. }
  27. }
  28. if (!parentEl) parentEl=document.getElementsByTagName('body')[0];
  29. if (!parentEl) return false;
  30. // inject
  31. var obj = swfCreate(
  32. {
  33. 'data': url,
  34. 'width': '2',
  35. 'height': '2',
  36. 'id': swfElementId,
  37. 'name': swfElementId,
  38. 'align': 'top'
  39. },
  40. {
  41. 'quality': 'low',
  42. 'bgcolor': 'transparent',
  43. 'allowscriptaccess': 'sameDomain',
  44. 'allowfullscreen': 'false'
  45. }
  46. );
  47. parentEl.appendChild(obj);
  48. swfRegisterUnloadHandler();
  49. return true;
  50. }
  51. function swfReady() {
  52. return swfHasLoaded;
  53. }
  54. function swfSetVolume(v) {
  55. if (wfHasLoaded) {
  56. var obj=document.getElementById(swfElementId);
  57. if (obj) el.setVolume(v);
  58. }
  59. swfVol=v;
  60. }
  61. function swfSpeak(txt, options) {
  62. if (swfHasLoaded && window.meSpeak) {
  63. var obj=document.getElementById(swfElementId);
  64. if (obj) {
  65. if (!typeof options != 'object') options={};
  66. options.rawdata='array';
  67. obj.play( meSpeak.speak(txt, options) );
  68. }
  69. }
  70. }
  71. function swf10Available() {
  72. return swfIsAvailable(10);
  73. }
  74. function swfFallbackHandshake() {
  75. swfHasLoaded=true;
  76. if (swfVol!=1) swfSetVolume(swfVol);
  77. if (window.console) console.log('meSpeak-SWF-fallback available.');
  78. }
  79. // private: a stripped-down version of swfobject.js
  80. function swfIsAvailable(leastMajorVersion) {
  81. // returns Boolean: flashplayer and version at least 10.x
  82. var sf='Shockwave Flash', sfm='application/x-shockwave-flash';
  83. if (navigator.plugins !== undefined && typeof navigator.plugins[sf] == 'object') {
  84. var d=navigator.plugins[sf].description;
  85. if (d && !(typeof navigator.mimeTypes !==undefined && navigator.mimeTypes[sfm] && !navigator.mimeTypes[sfm].enabledPlugin)) {
  86. d=d.replace(/^.*\s+(\S+\s+\S+$)/, '$1');
  87. if (leastMajorVersion<= parseInt(d.replace(/^(.*)\..*$/, '$1'), 10)) return true;
  88. }
  89. }
  90. else if (window.ActiveXObject) {
  91. try {
  92. var a=new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
  93. if (a) {
  94. swfViaAX=true;
  95. d=a.GetVariable('$version');
  96. if (d) {
  97. d=d.split(' ')[1].split(',');
  98. if (leastMajorVersion<= parseInt(d[0], 10)) return true;
  99. }
  100. }
  101. }
  102. catch(e) {}
  103. }
  104. return false;
  105. }
  106. function swfCreate(attributes, params) {
  107. if (swfViaAX) {
  108. var att='', par='', i;
  109. for (i in attributes) {
  110. var a=i.toLowerCase;
  111. if (a=='data') {
  112. params.movie=attributes[i];
  113. }
  114. else if (a=='styleclass') {
  115. att+=' class="'+attributes[i]+'"';
  116. }
  117. else if (a!='classid') {
  118. att+=' '+i+'="'+attributes[i]+'"';
  119. }
  120. }
  121. for (i in params) {
  122. if (params[i] != Object.prototype[i]) par+=' <param name="'+i+'" value="'+params[i]+'" />';
  123. }
  124. var el=document.createElement('div');
  125. el.outerHTML='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+att+'>'+par+'</object>';
  126. return el;
  127. }
  128. else {
  129. var o=document.createElement('object');
  130. o.setAttribute('type', 'application/x-shockwave-flash');
  131. for (var i in attributes) {
  132. if (attributes[i] != Object.prototype[i]) {
  133. var a=i.toLowerCase();
  134. if (a=='styleclass') {
  135. o.setAttribute('class', attributes[i]);
  136. }
  137. else if (a!='styleclass') {
  138. o.setAttribute(i, attributes[i]);
  139. }
  140. }
  141. }
  142. for (i in params) {
  143. if (attributes[i] != Object.prototype[i] && i.toLowerCase() != 'movie') {
  144. var p=document.createElement('param');
  145. p.setAttribute('name', i);
  146. p.setAttribute('value', attributes[i]);
  147. o.appendChild(p);
  148. }
  149. }
  150. return o;
  151. }
  152. }
  153. function swfRemove(obj) {
  154. try {
  155. if (typeof obj =='string') obj=document.getElementById(obj);
  156. if (!obj || typeof obj !='object') return;
  157. if (swfViaAX) {
  158. obj.style.display='none';
  159. swfRemoveObjectInIE(obj.id);
  160. }
  161. else if (obj.parentNode) {
  162. obj.parentNode.removeChild(obj);
  163. }
  164. swfInstalled=false;
  165. }
  166. catch(e) {}
  167. }
  168. function swfRemoveObjectInIE(id) {
  169. var obj=document.getElementById(obj);
  170. if (obj) {
  171. if (obj.readyState==4) {
  172. for (var i in obj) {
  173. if (typeof obj[i] =='function') obj[i] = null;
  174. }
  175. if (obj.parentNode) obj.parentNode.removeChild(obj);
  176. }
  177. else {
  178. setTimeout(function() {swfRemoveObjectInIE(id)}, 10);
  179. }
  180. }
  181. }
  182. function swfUnloadHandler() {
  183. if (swfElementId) swfRemove(swfElementId);
  184. if (!window.addEventListener && window.detachEvent) window.detachEvent('onunload', swfUnloadHandler);
  185. }
  186. function swfRegisterUnloadHandler() {
  187. if (window.addEventListener) {
  188. window.addEventListener('unload', swfUnloadHandler, false);
  189. }
  190. else if (window.attachEvent) {
  191. window.attachEvent('onunload', swfUnloadHandler);
  192. }
  193. }
  194. return {
  195. 'install': swfInstallFallback,
  196. 'isAvailable': swf10Available,
  197. 'ready': swfReady,
  198. 'speak': swfSpeak,
  199. 'setVolume': swfSetVolume,
  200. 'swfFallbackHandshake': swfFallbackHandshake
  201. }
  202. };
  203. function meSpeakFallbackHandshake() {
  204. // handshake handler with swf external interface
  205. meSpeakFlashFallback.swfFallbackHandshake();
  206. }