recorder.js 7.5 KB

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