recorder.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. var Wami = window.Wami || {};
  2. // Returns a (very likely) unique string with of random letters and numbers
  3. Wami.createID = function() {
  4. return "wid" + ("" + 1e10).replace(/[018]/g, function(a) {
  5. return (a ^ Math.random() * 16 >> a / 4).toString(16)
  6. });
  7. }
  8. // Creates a named callback in WAMI and returns the name as a string.
  9. Wami.nameCallback = function(cb, cleanup) {
  10. Wami._callbacks = Wami._callbacks || {};
  11. var id = Wami.createID();
  12. Wami._callbacks[id] = function() {
  13. if (cleanup) {
  14. Wami._callbacks[id] = null;
  15. }
  16. cb.apply(null, arguments);
  17. };
  18. var named = "Wami._callbacks['" + id + "']";
  19. return named;
  20. }
  21. // This method ensures that a WAMI recorder is operational, and that
  22. // the following API is available in the Wami namespace. All functions
  23. // must be named (i.e. cannot be anonymous).
  24. //
  25. // Wami.startPlaying(url, startfn = null, finishedfn = null, failedfn = null);
  26. // Wami.stopPlaying()
  27. //
  28. // Wami.startRecording(url, startfn = null, finishedfn = null, failedfn = null);
  29. // Wami.stopRecording()
  30. //
  31. // Wami.getRecordingLevel() // Returns a number between 0 and 100
  32. // Wami.getPlayingLevel() // Returns a number between 0 and 100
  33. //
  34. // Wami.hide()
  35. // Wami.show()
  36. //
  37. // Manipulate the WAMI recorder's settings. In Flash
  38. // we need to check if the microphone permission has been granted.
  39. // We might also set/return sample rate here, etc.
  40. //
  41. // Wami.getSettings();
  42. // Wami.setSettings(options);
  43. //
  44. // Optional way to set up browser so that it's constantly listening
  45. // This is to prepend audio in case the user starts talking before
  46. // they click-to-talk.
  47. //
  48. // Wami.startListening()
  49. //
  50. Wami.setup = function(options) {
  51. if (Wami.startRecording) {
  52. // Wami's already defined.
  53. if (options.onReady) {
  54. options.onReady();
  55. }
  56. return;
  57. }
  58. // Assumes that swfobject.js is included if Wami.swfobject isn't
  59. // already defined.
  60. Wami.swfobject = Wami.swfobject || swfobject;
  61. if (!Wami.swfobject) {
  62. alert("Unable to find swfobject to help embed the SWF.");
  63. }
  64. var _options;
  65. setOptions(options);
  66. embedWamiSWF(_options.id, Wami.nameCallback(delegateWamiAPI));
  67. function supportsTransparency() {
  68. // Detecting the OS is a big no-no in Javascript programming, but
  69. // I can't think of a better way to know if wmode is supported or
  70. // not... since NOT supporting it (like Flash on Ubuntu) is a bug.
  71. return (navigator.platform.indexOf("Linux") == -1);
  72. }
  73. function setOptions(options) {
  74. // Start with default options
  75. _options = {
  76. swfUrl : "Wami.swf",
  77. onReady : function() {
  78. Wami.hide();
  79. },
  80. onSecurity : checkSecurity,
  81. onError : function(error) {
  82. alert(error);
  83. }
  84. };
  85. if (typeof options == 'undefined') {
  86. alert('Need at least an element ID to place the Flash object.');
  87. }
  88. if (typeof options == 'string') {
  89. _options.id = options;
  90. } else {
  91. _options.id = options.id;
  92. }
  93. if (options.swfUrl) {
  94. _options.swfUrl = options.swfUrl;
  95. }
  96. if (options.onReady) {
  97. _options.onReady = options.onReady;
  98. }
  99. if (options.onLoaded) {
  100. _options.onLoaded = options.onLoaded;
  101. }
  102. if (options.onSecurity) {
  103. _options.onSecurity = options.onSecurity;
  104. }
  105. if (options.onError) {
  106. _options.onError = options.onError;
  107. }
  108. // Create a DIV for the SWF under _options.id
  109. var container = document.createElement('div');
  110. container.style.position = 'absolute';
  111. _options.cid = Wami.createID();
  112. container.setAttribute('id', _options.cid);
  113. var swfdiv = document.createElement('div');
  114. var id = Wami.createID();
  115. swfdiv.setAttribute('id', id);
  116. container.appendChild(swfdiv);
  117. document.getElementById(_options.id).appendChild(container);
  118. _options.id = id;
  119. }
  120. function checkSecurity() {
  121. var settings = Wami.getSettings();
  122. if (settings.microphone.granted) {
  123. _options.onReady();
  124. } else {
  125. // Show any Flash settings panel you want:
  126. // http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/SecurityPanel.html
  127. Wami.showSecurity("privacy", "Wami.show", Wami
  128. .nameCallback(_options.onSecurity), Wami
  129. .nameCallback(_options.onError));
  130. }
  131. }
  132. // Embed the WAMI SWF and call the named callback function when loaded.
  133. function embedWamiSWF(id, initfn) {
  134. var flashVars = {
  135. visible : false,
  136. loadedCallback : initfn
  137. }
  138. var params = {
  139. allowScriptAccess : "always"
  140. }
  141. if (supportsTransparency()) {
  142. params.wmode = "transparent";
  143. }
  144. if (typeof console !== 'undefined') {
  145. flashVars.console = true;
  146. }
  147. var version = '10.0.0';
  148. document.getElementById(id).innerHTML = "WAMI requires Flash "
  149. + version
  150. + " or greater<br />https://get.adobe.com/flashplayer/";
  151. // This is the minimum size due to the microphone security panel
  152. Wami.swfobject.embedSWF(_options.swfUrl, id, 214, 137, version, null,
  153. flashVars, params);
  154. // Without this line, Firefox has a dotted outline of the flash
  155. Wami.swfobject.createCSS("#" + id, "outline:none");
  156. }
  157. // To check if the microphone settings were 'remembered', we
  158. // must actually embed an entirely new Wami client and check
  159. // whether its microphone is granted. If it is, it was remembered.
  160. function checkRemembered(finishedfn) {
  161. var id = Wami.createID();
  162. var div = document.createElement('div');
  163. div.style.top = '-999px';
  164. div.style.left = '-999px';
  165. div.setAttribute('id', id);
  166. var body = document.getElementsByTagName('body').item(0);
  167. body.appendChild(div);
  168. var fn = Wami.nameCallback(function() {
  169. var swf = document.getElementById(id);
  170. Wami._remembered = swf.getSettings().microphone.granted;
  171. Wami.swfobject.removeSWF(id);
  172. eval(finishedfn + "()");
  173. });
  174. embedWamiSWF(id, fn);
  175. }
  176. // Attach all the audio methods to the Wami namespace in the callback.
  177. function delegateWamiAPI() {
  178. var recorder = document.getElementById(_options.id);
  179. function delegate(name) {
  180. Wami[name] = function() {
  181. return recorder[name].apply(recorder, arguments);
  182. }
  183. }
  184. delegate('startPlaying');
  185. delegate('stopPlaying');
  186. delegate('startRecording');
  187. delegate('stopRecording');
  188. delegate('startListening');
  189. delegate('stopListening');
  190. delegate('getRecordingLevel');
  191. delegate('getPlayingLevel');
  192. delegate('setSettings');
  193. // Append extra information about whether mic settings are sticky
  194. Wami.getSettings = function() {
  195. var settings = recorder.getSettings();
  196. settings.microphone.remembered = Wami._remembered;
  197. return settings;
  198. }
  199. Wami.showSecurity = function(panel, startfn, finishedfn, failfn) {
  200. // Flash must be on top for this.
  201. var container = document.getElementById(_options.cid);
  202. var augmentedfn = Wami.nameCallback(function() {
  203. checkRemembered(finishedfn);
  204. container.style.cssText = "position: absolute;";
  205. });
  206. container.style.cssText = "position: absolute; z-index: 99999";
  207. recorder.showSecurity(panel, startfn, augmentedfn, failfn);
  208. }
  209. Wami.show = function() {
  210. if (!supportsTransparency()) {
  211. recorder.style.visibility = "visible";
  212. }
  213. }
  214. Wami.hide = function() {
  215. // Hiding flash in all the browsers is tricky. Please read:
  216. // https://code.google.com/p/wami-recorder/wiki/HidingFlash
  217. if (!supportsTransparency()) {
  218. recorder.style.visibility = "hidden";
  219. }
  220. }
  221. // If we already have permissions, they were previously 'remembered'
  222. Wami._remembered = recorder.getSettings().microphone.granted;
  223. if (_options.onLoaded) {
  224. _options.onLoaded();
  225. }
  226. if (!_options.noSecurityCheck) {
  227. checkSecurity();
  228. }
  229. }
  230. }