Guest User

Untitled

a guest
Jan 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import "@babel/polyfill";
  2. import React, { useEffect, useRef, useLayoutEffect } from "react";
  3. import ReactDOM from "react-dom";
  4. import "firebase/storage";
  5. import firebase from "firebase/app";
  6.  
  7. firebase.initializeApp({/* your firebase config */});
  8.  
  9. declare var MediaRecorder: any;
  10.  
  11. function Recorder() {
  12. const audioRef = useRef<HTMLAudioElement>(null);
  13.  
  14. useLayoutEffect(() => {
  15. (async () => {
  16. if (audioRef.current) {
  17. const audio = audioRef.current;
  18.  
  19. const stream = await navigator.mediaDevices.getUserMedia({
  20. audio: true,
  21. video: false
  22. });
  23. const recorder = new MediaRecorder(stream);
  24. let chunks: Array<any> = [];
  25. recorder.addEventListener("dataavailable", (e: any) => {
  26. chunks.push(e.data);
  27. console.log("on data", e.data);
  28. });
  29.  
  30. recorder.addEventListener("stop", async () => {
  31. const blob = new Blob(chunks, { type: "audio/webm" });
  32. audio.src = window.URL.createObjectURL(blob);
  33. audio.play();
  34.  
  35. // firebase uploading
  36. const storageRef = firebase.storage().ref();
  37. const tempRef = storageRef.child("temp.webm");
  38. const snapshot = await tempRef.put(blob);
  39. console.log("uploaded!", snapshot);
  40. });
  41.  
  42. recorder.start();
  43. await new Promise(r => setTimeout(r, 5 * 1000));
  44. recorder.stop();
  45. }
  46. })();
  47. }, []);
  48.  
  49. return (
  50. <div>
  51. recorder
  52. <audio controls ref={audioRef} />
  53. </div>
  54. );
  55. }
  56.  
  57. ReactDOM.render(<Recorder />, document.querySelector(".root"));
Add Comment
Please, Sign In to add comment