cameraDemo.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <div>
  3. <el-button type="primary" @click="startRecording" v-if="!videoStart">开始录制</el-button>
  4. <el-button type="primary" @click="stopRecording" v-else>停止录制</el-button>
  5. </div>
  6. </template>
  7. <script>
  8. import RecordRTC from 'recordrtc';
  9. export default {
  10. data() {
  11. return {
  12. videoStart: false,
  13. recorder: null,
  14. fileName: "",
  15. }
  16. },
  17. props: {
  18. // fileName:{
  19. // type:String,
  20. // default:new Date().getTime()
  21. // }
  22. },
  23. onLoad() {
  24. },
  25. mounted() {
  26. },
  27. methods: {
  28. getAudioVideo(constraintsData) {
  29. if (navigator.mediaDevices === undefined) {
  30. navigator.mediaDevices = {};
  31. }
  32. if (navigator.mediaDevices.getUserMedia === undefined) {
  33. navigator.mediaDevices.getUserMedia = function (constraints) {
  34. // 首先,如果有getUserMedia的话,就获得它
  35. var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
  36. // 一些浏览器根本没实现它 - 那么就返回一个error到promise的reject来保持一个统一的接口
  37. if (!getUserMedia) {
  38. return Promise.reject({ code: 404 });
  39. }
  40. // 否则,为老的navigator.getUserMedia方法包裹一个Promise
  41. return new Promise(function (resolve, reject) {
  42. getUserMedia.call(navigator, constraints, resolve, reject);
  43. });
  44. }
  45. }
  46. return navigator.mediaDevices.getUserMedia(constraintsData)
  47. },
  48. /**
  49. * 开始录制
  50. */
  51. startRecording(callback) {
  52. let _function = (screenStream) => {
  53. this.addStreamStopListener(screenStream, () => {
  54. console.log("流停止监听");
  55. this.stopRecording();
  56. this.$emit("streamStop", {})
  57. });
  58. var options = {
  59. type: 'video',
  60. mimeType: 'video/webm',
  61. disableLogs: false,
  62. getNativeBlob: false, // enable it for longer recordings
  63. ignoreMutedMedia: false
  64. };
  65. // this.video.srcObject = screenStream;
  66. this.recorder = RecordRTC(screenStream, options);
  67. this.recorder.startRecording();
  68. this.recorder.screen = screenStream;
  69. this.videoStart = true;
  70. // callback(true);
  71. }
  72. this.getAudioVideo({ audio: true }).then(res => {
  73. this.captureScreen(_function, true);
  74. console.log('已点击允许,开启成功');
  75. }).catch(err => {
  76. // if (err.code && err.code == 404) {
  77. // console.log('浏览器不支持,请更换浏览器')
  78. // } else {
  79. this.captureScreen(_function, false);
  80. console.log('请检查是否存在麦克风')
  81. // }
  82. })
  83. },
  84. /**
  85. * 停止录制
  86. */
  87. stopRecording(callback) {
  88. this.recorder.stopRecording(() => {
  89. // this.video.src = this.video.srcObject = null;
  90. // this.video.src = URL.createObjectURL(this.recorder.getBlob());
  91. const url = URL.createObjectURL(this.recorder.getBlob());
  92. const a = document.createElement("a");
  93. let videoFile = new File([this.recorder.getBlob()], this.fileName + ".mp4", {
  94. type: 'video/mp4'
  95. })
  96. let downloadUrl = URL.createObjectURL(videoFile);
  97. document.body.appendChild(a);
  98. a.style.display = "none";
  99. a.href = url;
  100. a.download = this.fileName + ".mp4";
  101. a.click();
  102. this.recorder.screen.stop();
  103. this.recorder.destroy();
  104. this.recorder = null;
  105. this.videoStart = false;
  106. // callback(false);
  107. });
  108. },
  109. //初始化
  110. captureScreen(callback, type) {
  111. if (navigator.getDisplayMedia) {
  112. //录制结束,文件下载
  113. navigator.getDisplayMedia({
  114. video: true
  115. }).then(screenStream => {
  116. if (type) {
  117. navigator.mediaDevices.getUserMedia({ audio: true }).then((mic) => {
  118. screenStream.addTrack(mic.getTracks()[0]);
  119. callback(screenStream);
  120. });
  121. } else {
  122. callback(screenStream);
  123. }
  124. }).catch(function (error) {
  125. console.log('error', error);
  126. });
  127. } else if (navigator.mediaDevices.getDisplayMedia) {
  128. navigator.mediaDevices.getDisplayMedia({
  129. video: true
  130. }).then(screenStream => {
  131. if (type) {
  132. navigator.mediaDevices.getUserMedia({ audio: true }).then((mic) => {
  133. screenStream.addTrack(mic.getTracks()[0]);
  134. callback(screenStream);
  135. });
  136. } else {
  137. callback(screenStream);
  138. }
  139. }).catch(function (error) {
  140. console.log('error', error);
  141. });
  142. } else {
  143. var error = 'getDisplayMedia API are not supported in this browser.';
  144. console.log('error', error);
  145. alert(error);
  146. }
  147. },
  148. //流监听
  149. addStreamStopListener(stream, callback) {
  150. stream.addEventListener('ended', function () {
  151. callback();
  152. callback = function () { };
  153. }, false);
  154. stream.addEventListener('inactive', function () {
  155. callback();
  156. callback = function () { };
  157. }, false);
  158. stream.getTracks().forEach(function (track) {
  159. track.addEventListener('ended', function () {
  160. callback();
  161. callback = function () { };
  162. }, false);
  163. track.addEventListener('inactive', function () {
  164. callback();
  165. callback = function () { };
  166. }, false);
  167. });
  168. },
  169. }
  170. }
  171. </script>
  172. <style>
  173. </style>