@@ -2,7 +2,6 @@ import RecordRTC from "recordrtc";
22
33function init ( Survey ) {
44 var widget = {
5-
65 name : "microphone" ,
76 title : "Microphone" ,
87 iconName : "icon-microphone" ,
@@ -13,157 +12,149 @@ function init(Survey) {
1312 return question . getType ( ) === "microphone" ;
1413 } ,
1514 htmlTemplate :
16- "<div>" +
17- "<button type='button' title='Record'><i class='fa fa-microphone' aria-hidden='true'></i></button>" +
18- " <button type='button' title='Save'><i class='fa fa-cloud' aria-hidden='true'></i></button>" +
19- " <audio style='" +
20- "position:relative; " +
21- "top:16px; " +
22- "left:10px; " +
23- "height:35px;" +
24- "-moz-box-shadow: 2px 2px 4px 0px #006773;" +
25- "-webkit-box-shadow: 2px 2px 4px 0px #006773;" +
26- "box-shadow: 2px 2px 4px 0px #006773;" +
27- "' " +
28- "controls='true' >" +
29- "</audio>" +
30- "</div>" ,
15+ "<div style='height: 39px'>" +
16+ "<button type='button' title='Record' style='vertical-align: top; margin-top: 3px' ><i class='fa fa-microphone' aria-hidden='true'></i></button>" +
17+ " <button type='button' title='Save' style='vertical-align: top; margin-top: 3px'><i class='fa fa-cloud' aria-hidden='true' ></i></button>" +
18+ " <audio style='" +
19+ "vertical-align: top;" +
20+ "margin-left: 10px;" +
21+ "height:35px;" +
22+ "-moz-box-shadow: 2px 2px 4px 0px #006773;" +
23+ "-webkit-box-shadow: 2px 2px 4px 0px #006773;" +
24+ "box-shadow: 2px 2px 4px 0px #006773;" +
25+ "' " +
26+ "controls='true' >" +
27+ "</audio>" +
28+ "</div>" ,
3129 activatedByChanged : function ( activatedBy ) {
3230 Survey . JsonObject . metaData . addClass ( "microphone" , [ ] , null , "empty" ) ;
3331 } ,
34-
32+
3533 afterRender : function ( question , el ) {
3634 var rootWidget = this ;
37- var buttonStartEl = el . getElementsByTagName ( "button" ) [ 0 ] ;
38- var buttonStopEl = el . getElementsByTagName ( "button" ) [ 1 ] ;
39- var audioEl = el . getElementsByTagName ( "audio" ) [ 0 ] ;
40-
41- ////////// RecordRTC logic
42-
43- var successCallback = function ( stream ) {
44- var options = {
45- type : 'audio' ,
46- mimeType : 'audio/webm' ,
47- audioBitsPerSecond : 44100 ,
48- sampleRate : 44100 ,
49- bufferSize : 16384 ,
50- numberOfAudioChannels : 1
51- } ;
52- console . log ( "successCallback" ) ;
53- question . survey . mystream = stream ;
54- question . survey . recordRTC = RecordRTC ( question . survey . mystream , options ) ;
55- if ( typeof question . survey . recordRTC != "undefined" ) {
56- console . log ( "startRecording" ) ;
57- question . survey . recordRTC . startRecording ( ) ;
58- }
59- } ;
60-
61- var errorCallback = function ( ) {
62- alert ( 'No microphone' ) ;
63- question . survey . recordRTC = undefined ;
64- question . survey . mystream = undefined ;
65- } ;
66-
67- var processAudio = function ( audioVideoWebMURL ) {
68- console . log ( "processAudio" ) ;
69- var recordedBlob = question . survey . recordRTC . getBlob ( ) ;
70-
71- var fileReader = new FileReader ( ) ;
72- fileReader . onload = function ( event ) {
73- var dataUri = event . target . result ;
74- console . log ( "dataUri: " + dataUri ) ;
75- question . value = dataUri ;
76- audioEl . src = dataUri ;
77-
78- console . log ( "cleaning" ) ;
79- question . survey . recordRTC = undefined ;
80- question . survey . mystream = undefined ;
35+ var buttonStartEl = el . getElementsByTagName ( "button" ) [ 0 ] ;
36+ var buttonStopEl = el . getElementsByTagName ( "button" ) [ 1 ] ;
37+ var audioEl = el . getElementsByTagName ( "audio" ) [ 0 ] ;
38+
39+ ////////// RecordRTC logic
40+
41+ var successCallback = function ( stream ) {
42+ var options = {
43+ type : "audio" ,
44+ mimeType : "audio/webm" ,
45+ audioBitsPerSecond : 44100 ,
46+ sampleRate : 44100 ,
47+ bufferSize : 16384 ,
48+ numberOfAudioChannels : 1
49+ } ;
50+ console . log ( "successCallback" ) ;
51+ question . survey . mystream = stream ;
52+ question . survey . recordRTC = RecordRTC (
53+ question . survey . mystream ,
54+ options
55+ ) ;
56+ if ( typeof question . survey . recordRTC != "undefined" ) {
57+ console . log ( "startRecording" ) ;
58+ question . survey . recordRTC . startRecording ( ) ;
59+ }
60+ } ;
61+
62+ var errorCallback = function ( ) {
63+ alert ( "No microphone" ) ;
64+ question . survey . recordRTC = undefined ;
65+ question . survey . mystream = undefined ;
66+ } ;
67+
68+ var processAudio = function ( audioVideoWebMURL ) {
69+ console . log ( "processAudio" ) ;
70+ var recordedBlob = question . survey . recordRTC . getBlob ( ) ;
71+
72+ var fileReader = new FileReader ( ) ;
73+ fileReader . onload = function ( event ) {
74+ var dataUri = event . target . result ;
75+ console . log ( "dataUri: " + dataUri ) ;
76+ question . value = dataUri ;
77+ audioEl . src = dataUri ;
78+
79+ console . log ( "cleaning" ) ;
80+ question . survey . recordRTC = undefined ;
81+ question . survey . mystream = undefined ;
8182 } ;
8283 fileReader . readAsDataURL ( recordedBlob ) ;
83- } ;
84-
85- var startRecording = function ( ) {
86-
87- // erase previous data
88- question . value = undefined ;
89-
90- // if recorder open on another question - try to stop recording
91- if ( typeof question . survey . recordRTC != "undefined" ) {
92- question . survey . recordRTC . stopRecording ( doNothingHandler ) ;
93- if ( typeof question . survey . mystream != "undefined" ) {
94- question . survey . mystream . getAudioTracks ( ) . forEach ( function ( track ) {
95- track . stop ( ) ;
96- }
97- ) ;
98- }
99- }
100-
101- var mediaConstraints = {
102- video : false ,
103- audio : true
104- } ;
105-
106- navigator . mediaDevices
107- . getUserMedia ( mediaConstraints )
108- . then ( successCallback . bind ( this ) , errorCallback . bind ( this ) ) ;
109- } ;
110-
111- var stopRecording = function ( ) {
112- console . log ( "stopRecording" ) ;
113- if ( typeof question . survey . recordRTC != "undefined" ) {
114- question . survey . recordRTC . stopRecording ( processAudio . bind ( this ) ) ;
115- if ( typeof question . survey . mystream != "undefined" ) {
116- question . survey . mystream . getAudioTracks ( ) . forEach ( function ( track ) {
117- track . stop ( ) ;
118- }
119- ) ;
120- }
121- }
122- } ;
123-
124- ////////////// end RTC logic //////////////////
125-
126- if ( ! question . isReadOnly ) {
84+ } ;
85+
86+ var startRecording = function ( ) {
87+ // erase previous data
88+ question . value = undefined ;
89+
90+ // if recorder open on another question - try to stop recording
91+ if ( typeof question . survey . recordRTC != "undefined" ) {
92+ question . survey . recordRTC . stopRecording ( doNothingHandler ) ;
93+ if ( typeof question . survey . mystream != "undefined" ) {
94+ question . survey . mystream . getAudioTracks ( ) . forEach ( function ( track ) {
95+ track . stop ( ) ;
96+ } ) ;
97+ }
98+ }
99+
100+ var mediaConstraints = {
101+ video : false ,
102+ audio : true
103+ } ;
104+
105+ navigator . mediaDevices
106+ . getUserMedia ( mediaConstraints )
107+ . then ( successCallback . bind ( this ) , errorCallback . bind ( this ) ) ;
108+ } ;
109+
110+ var stopRecording = function ( ) {
111+ console . log ( "stopRecording" ) ;
112+ if ( typeof question . survey . recordRTC != "undefined" ) {
113+ question . survey . recordRTC . stopRecording ( processAudio . bind ( this ) ) ;
114+ if ( typeof question . survey . mystream != "undefined" ) {
115+ question . survey . mystream . getAudioTracks ( ) . forEach ( function ( track ) {
116+ track . stop ( ) ;
117+ } ) ;
118+ }
119+ }
120+ } ;
121+
122+ ////////////// end RTC logic //////////////////
123+
124+ if ( ! question . isReadOnly ) {
127125 buttonStartEl . onclick = startRecording ;
128126 } else {
129127 buttonStartEl . parentNode . removeChild ( buttonStartEl ) ;
130128 }
131-
132- if ( ! question . isReadOnly ) {
129+
130+ if ( ! question . isReadOnly ) {
133131 buttonStopEl . onclick = stopRecording ;
134132 } else {
135133 buttonStopEl . parentNode . removeChild ( buttonStopEl ) ;
136134 }
137-
138-
139- audioEl . src = question . value
140-
141- var updateValueHandler = function ( ) {
142-
143- } ;
144-
145- var doNothingHandler = function ( ) {
146-
147- } ;
148-
135+
136+ audioEl . src = question . value ;
137+
138+ var updateValueHandler = function ( ) { } ;
139+
140+ var doNothingHandler = function ( ) { } ;
141+
149142 question . valueChangedCallback = updateValueHandler ;
150143 updateValueHandler ( ) ;
151-
152-
153144 } ,
154145 willUnmount : function ( question , el ) {
155146 console . log ( "unmount microphone no record " ) ;
156- if ( typeof question . survey . recordRTC != "undefined" ) {
157- question . survey . recordRTC . stopRecording ( doNothingHandler ) ;
158- if ( typeof question . survey . mystream != "undefined" ) {
159- question . survey . mystream . getAudioTracks ( ) . forEach ( function ( track ) {
160- track . stop ( ) ;
161- } ) ;
162- }
163- question . value = undefined ;
164- question . survey . recordRTC = undefined ;
165- question . survey . mystream = undefined ;
166- }
147+ if ( typeof question . survey . recordRTC != "undefined" ) {
148+ question . survey . recordRTC . stopRecording ( doNothingHandler ) ;
149+ if ( typeof question . survey . mystream != "undefined" ) {
150+ question . survey . mystream . getAudioTracks ( ) . forEach ( function ( track ) {
151+ track . stop ( ) ;
152+ } ) ;
153+ }
154+ question . value = undefined ;
155+ question . survey . recordRTC = undefined ;
156+ question . survey . mystream = undefined ;
157+ }
167158 }
168159 } ;
169160
0 commit comments