recorder.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. // Chamilo change
  73. return true;
  74. }
  75. function setOptions(options) {
  76. // Start with default options
  77. _options = {
  78. swfUrl : "Wami.swf",
  79. onReady : function() {
  80. Wami.hide();
  81. },
  82. onSecurity : checkSecurity,
  83. onError : function(error) {
  84. alert(error);
  85. }
  86. };
  87. if (typeof options == 'undefined') {
  88. alert('Need at least an element ID to place the Flash object.');
  89. }
  90. if (typeof options == 'string') {
  91. _options.id = options;
  92. } else {
  93. _options.id = options.id;
  94. }
  95. if (options.swfUrl) {
  96. _options.swfUrl = options.swfUrl;
  97. }
  98. if (options.onReady) {
  99. _options.onReady = options.onReady;
  100. }
  101. if (options.onLoaded) {
  102. _options.onLoaded = options.onLoaded;
  103. }
  104. if (options.onSecurity) {
  105. _options.onSecurity = options.onSecurity;
  106. }
  107. if (options.onError) {
  108. _options.onError = options.onError;
  109. }
  110. // Create a DIV for the SWF under _options.id
  111. var container = document.createElement('div');
  112. container.style.position = 'absolute';
  113. _options.cid = Wami.createID();
  114. container.setAttribute('id', _options.cid);
  115. var swfdiv = document.createElement('div');
  116. var id = Wami.createID();
  117. swfdiv.setAttribute('id', id);
  118. container.appendChild(swfdiv);
  119. document.getElementById(_options.id).appendChild(container);
  120. _options.id = id;
  121. }
  122. function checkSecurity() {
  123. var settings = Wami.getSettings();
  124. if (settings.microphone.granted) {
  125. _options.onReady();
  126. } else {
  127. // Show any Flash settings panel you want:
  128. // http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/SecurityPanel.html
  129. Wami.showSecurity("privacy", "Wami.show", Wami
  130. .nameCallback(_options.onSecurity), Wami
  131. .nameCallback(_options.onError));
  132. }
  133. }
  134. // Embed the WAMI SWF and call the named callback function when loaded.
  135. function embedWamiSWF(id, initfn) {
  136. var flashVars = {
  137. visible : false,
  138. loadedCallback : initfn
  139. }
  140. var params = {
  141. allowScriptAccess : "always"
  142. }
  143. if (supportsTransparency()) {
  144. params.wmode = "transparent";
  145. }
  146. if (typeof console !== 'undefined') {
  147. flashVars.console = true;
  148. }
  149. var version = '10.0.0';
  150. document.getElementById(id).innerHTML = "WAMI requires Flash "
  151. + version
  152. + " or greater<br />https://get.adobe.com/flashplayer/";
  153. // This is the minimum size due to the microphone security panel
  154. Wami.swfobject.embedSWF(_options.swfUrl, id, 214, 137, version, null,
  155. flashVars, params);
  156. // Without this line, Firefox has a dotted outline of the flash
  157. Wami.swfobject.createCSS("#" + id, "outline:none");
  158. }
  159. // To check if the microphone settings were 'remembered', we
  160. // must actually embed an entirely new Wami client and check
  161. // whether its microphone is granted. If it is, it was remembered.
  162. function checkRemembered(finishedfn) {
  163. var id = Wami.createID();
  164. var div = document.createElement('div');
  165. div.style.top = '-999px';
  166. div.style.left = '-999px';
  167. div.setAttribute('id', id);
  168. var body = document.getElementsByTagName('body').item(0);
  169. body.appendChild(div);
  170. var fn = Wami.nameCallback(function() {
  171. var swf = document.getElementById(id);
  172. Wami._remembered = swf.getSettings().microphone.granted;
  173. Wami.swfobject.removeSWF(id);
  174. eval(finishedfn + "()");
  175. });
  176. embedWamiSWF(id, fn);
  177. }
  178. // Attach all the audio methods to the Wami namespace in the callback.
  179. function delegateWamiAPI() {
  180. var recorder = document.getElementById(_options.id);
  181. function delegate(name) {
  182. Wami[name] = function() {
  183. return recorder[name].apply(recorder, arguments);
  184. }
  185. }
  186. delegate('startPlaying');
  187. delegate('stopPlaying');
  188. delegate('startRecording');
  189. delegate('stopRecording');
  190. delegate('startListening');
  191. delegate('stopListening');
  192. delegate('getRecordingLevel');
  193. delegate('getPlayingLevel');
  194. delegate('setSettings');
  195. // Append extra information about whether mic settings are sticky
  196. Wami.getSettings = function() {
  197. var settings = recorder.getSettings();
  198. settings.microphone.remembered = Wami._remembered;
  199. return settings;
  200. }
  201. Wami.showSecurity = function(panel, startfn, finishedfn, failfn) {
  202. // Flash must be on top for this.
  203. var container = document.getElementById(_options.cid);
  204. var augmentedfn = Wami.nameCallback(function() {
  205. checkRemembered(finishedfn);
  206. container.style.cssText = "position: absolute;";
  207. });
  208. container.style.cssText = "position: absolute; z-index: 99999";
  209. recorder.showSecurity(panel, startfn, augmentedfn, failfn);
  210. }
  211. Wami.show = function() {
  212. if (!supportsTransparency()) {
  213. recorder.style.visibility = "visible";
  214. }
  215. }
  216. Wami.hide = function() {
  217. // Hiding flash in all the browsers is tricky. Please read:
  218. // https://code.google.com/p/wami-recorder/wiki/HidingFlash
  219. if (!supportsTransparency()) {
  220. recorder.style.visibility = "hidden";
  221. }
  222. }
  223. // If we already have permissions, they were previously 'remembered'
  224. Wami._remembered = recorder.getSettings().microphone.granted;
  225. if (_options.onLoaded) {
  226. _options.onLoaded();
  227. }
  228. if (!_options.noSecurityCheck) {
  229. checkSecurity();
  230. }
  231. }
  232. }