webrtcapi-asr.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>WebRTC webcam</title>
  7. <style>
  8. button {
  9. padding: 8px 16px;
  10. }
  11. video {
  12. width: 100%;
  13. }
  14. .option {
  15. margin-bottom: 8px;
  16. }
  17. #media {
  18. max-width: 1280px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="option">
  24. <input id="use-stun" type="checkbox"/>
  25. <label for="use-stun">Use STUN server</label>
  26. </div>
  27. <button id="start" onclick="start()">Start</button>
  28. <button id="stop" style="display: none" onclick="stop()">Stop</button>
  29. <button class="btn btn-primary" id="btn_start_record">Start Recording</button>
  30. <button class="btn btn-primary" id="btn_stop_record" disabled>Stop Recording</button>
  31. <!-- <button class="btn btn-primary" id="btn_download">Download Video</button> -->
  32. <input type="hidden" id="sessionid" value="0">
  33. <form class="form-inline" id="echo-form">
  34. <div class="form-group">
  35. <p>input text</p>
  36. <textarea cols="2" rows="3" style="width:600px;height:50px;" class="form-control" id="message">test</textarea>
  37. </div>
  38. <button type="submit" class="btn btn-default">Send</button>
  39. </form>
  40. <div id="media">
  41. <h2>Media</h2>
  42. <audio id="audio" autoplay="true"></audio>
  43. <video id="video" style="width:600px;" autoplay="true" playsinline="true"></video>
  44. </div>
  45. <iframe src="asr/index.html" width="600" height="500"></iframe>
  46. <script src="client.js"></script>
  47. <script type="text/javascript" src="http://cdn.sockjs.org/sockjs-0.3.4.js"></script>
  48. <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
  49. </body>
  50. <script type="text/javascript" charset="utf-8">
  51. $(document).ready(function() {
  52. // var host = window.location.hostname
  53. // var ws = new WebSocket("ws://"+host+":8000/humanecho");
  54. // //document.getElementsByTagName("video")[0].setAttribute("src", aa["video"]);
  55. // ws.onopen = function() {
  56. // console.log('Connected');
  57. // };
  58. // ws.onmessage = function(e) {
  59. // console.log('Received: ' + e.data);
  60. // data = e
  61. // var vid = JSON.parse(data.data);
  62. // console.log(typeof(vid),vid)
  63. // //document.getElementsByTagName("video")[0].setAttribute("src", vid["video"]);
  64. // };
  65. // ws.onclose = function(e) {
  66. // console.log('Closed');
  67. // };
  68. $('#echo-form').on('submit', function(e) {
  69. e.preventDefault();
  70. var message = $('#message').val();
  71. console.log('Sending: ' + message);
  72. console.log('sessionid: ',document.getElementById('sessionid').value);
  73. fetch('/human', {
  74. body: JSON.stringify({
  75. text: message,
  76. type: 'echo',
  77. interrupt: true,
  78. sessionid:parseInt(document.getElementById('sessionid').value),
  79. }),
  80. headers: {
  81. 'Content-Type': 'application/json'
  82. },
  83. method: 'POST'
  84. });
  85. //ws.send(message);
  86. $('#message').val('');
  87. });
  88. $('#btn_start_record').click(function() {
  89. // 开始录制
  90. console.log('Starting recording...');
  91. fetch('/record', {
  92. body: JSON.stringify({
  93. type: 'start_record',
  94. }),
  95. headers: {
  96. 'Content-Type': 'application/json'
  97. },
  98. method: 'POST'
  99. }).then(function(response) {
  100. if (response.ok) {
  101. console.log('Recording started.');
  102. $('#btn_start_record').prop('disabled', true);
  103. $('#btn_stop_record').prop('disabled', false);
  104. // $('#btn_download').prop('disabled', true);
  105. } else {
  106. console.error('Failed to start recording.');
  107. }
  108. }).catch(function(error) {
  109. console.error('Error:', error);
  110. });
  111. });
  112. $('#btn_stop_record').click(function() {
  113. // 结束录制
  114. console.log('Stopping recording...');
  115. fetch('/record', {
  116. body: JSON.stringify({
  117. type: 'end_record',
  118. }),
  119. headers: {
  120. 'Content-Type': 'application/json'
  121. },
  122. method: 'POST'
  123. }).then(function(response) {
  124. if (response.ok) {
  125. console.log('Recording stopped.');
  126. $('#btn_start_record').prop('disabled', false);
  127. $('#btn_stop_record').prop('disabled', true);
  128. // $('#btn_download').prop('disabled', false);
  129. } else {
  130. console.error('Failed to stop recording.');
  131. }
  132. }).catch(function(error) {
  133. console.error('Error:', error);
  134. });
  135. });
  136. // $('#btn_download').click(function() {
  137. // // 下载视频文件
  138. // console.log('Downloading video...');
  139. // fetch('/record_lasted.mp4', {
  140. // method: 'GET'
  141. // }).then(function(response) {
  142. // if (response.ok) {
  143. // return response.blob();
  144. // } else {
  145. // throw new Error('Failed to download the video.');
  146. // }
  147. // }).then(function(blob) {
  148. // // 创建一个 Blob 对象
  149. // const url = window.URL.createObjectURL(blob);
  150. // // 创建一个隐藏的可下载链接
  151. // const a = document.createElement('a');
  152. // a.style.display = 'none';
  153. // a.href = url;
  154. // a.download = 'record_lasted.mp4';
  155. // document.body.appendChild(a);
  156. // // 触发下载
  157. // a.click();
  158. // // 清理
  159. // window.URL.revokeObjectURL(url);
  160. // document.body.removeChild(a);
  161. // console.log('Video downloaded successfully.');
  162. // }).catch(function(error) {
  163. // console.error('Error:', error);
  164. // });
  165. // });
  166. });
  167. </script>
  168. </html>