{"ast":null,"code":"var _s = $RefreshSig$();\nimport { useState } from 'react';\n\n/**\r\n * Custom hook for recording audio using the browser's MediaRecorder API.\r\n * @returns An object containing functions to start and stop recording, as well as the URL of the recorded audio.\r\n */\nconst useAudioRecorder = () => {\n  _s();\n  const [audioStream, setAudioStream] = useState(null);\n  const [mediaRecorder, setMediaRecorder] = useState(null);\n  const [recordedAudioURL, setRecordedAudioURL] = useState(null);\n\n  /**\r\n   * Starts recording audio using the browser's MediaRecorder API.\r\n   * @throws {Error} If there is an error accessing the microphone.\r\n   */\n  const startRecording = async () => {\n    try {\n      const stream = await navigator.mediaDevices.getUserMedia({\n        audio: true\n      });\n      const recorder = new MediaRecorder(stream);\n      setAudioStream(stream);\n      setMediaRecorder(recorder);\n      const chunks = [];\n      recorder.ondataavailable = e => chunks.push(e.data);\n      recorder.onstop = () => {\n        const blob = new Blob(chunks, {\n          type: 'audio/webm'\n        });\n        const url = URL.createObjectURL(blob);\n        setRecordedAudioURL(url);\n      };\n      recorder.start();\n    } catch (error) {\n      console.error('Error accessing microphone:', error);\n    }\n  };\n\n  /**\r\n   * Stops recording audio and releases the microphone.\r\n   */\n  const stopRecording = () => {\n    if (mediaRecorder && audioStream) {\n      mediaRecorder.stop();\n      audioStream.getTracks().forEach(track => track.stop());\n    }\n  };\n  return {\n    startRecording,\n    stopRecording,\n    recordedAudioURL // URL of the recorded audio to play the recording\n  };\n};\n_s(useAudioRecorder, \"TaVi3FCZ6KTpA0y0/6LfCqpDYjc=\");\nexport default useAudioRecorder;","map":{"version":3,"names":["useState","useAudioRecorder","_s","audioStream","setAudioStream","mediaRecorder","setMediaRecorder","recordedAudioURL","setRecordedAudioURL","startRecording","stream","navigator","mediaDevices","getUserMedia","audio","recorder","MediaRecorder","chunks","ondataavailable","e","push","data","onstop","blob","Blob","type","url","URL","createObjectURL","start","error","console","stopRecording","stop","getTracks","forEach","track"],"sources":["D:/Project/UC_Trains_Voice/react-demo/src/function/AudioRecorder.ts"],"sourcesContent":["import React, { useState } from 'react';\r\n\r\n/**\r\n * Custom hook for recording audio using the browser's MediaRecorder API.\r\n * @returns An object containing functions to start and stop recording, as well as the URL of the recorded audio.\r\n */\r\nconst useAudioRecorder = () => {\r\n    const [audioStream, setAudioStream] = useState<MediaStream | null>(null);\r\n    const [mediaRecorder, setMediaRecorder] = useState<MediaRecorder | null>(null);\r\n    const [recordedAudioURL, setRecordedAudioURL] = useState<string | null>(null);\r\n    \r\n    /**\r\n     * Starts recording audio using the browser's MediaRecorder API.\r\n     * @throws {Error} If there is an error accessing the microphone.\r\n     */\r\n    const startRecording = async () => {\r\n      try {\r\n        const stream = await navigator.mediaDevices.getUserMedia({ audio: true });\r\n        const recorder = new MediaRecorder(stream);\r\n  \r\n        setAudioStream(stream);\r\n        setMediaRecorder(recorder);\r\n  \r\n        const chunks: Blob[] = [];\r\n        recorder.ondataavailable = (e) => chunks.push(e.data as Blob);\r\n        recorder.onstop = () => {\r\n          const blob = new Blob(chunks, { type: 'audio/webm' });\r\n          const url = URL.createObjectURL(blob);\r\n          setRecordedAudioURL(url);\r\n        };\r\n  \r\n        recorder.start();\r\n      } catch (error) {\r\n        console.error('Error accessing microphone:', error);\r\n      }\r\n    };\r\n    \r\n    /**\r\n     * Stops recording audio and releases the microphone.\r\n     */\r\n    const stopRecording = () => {\r\n      if (mediaRecorder && audioStream) {\r\n        mediaRecorder.stop();\r\n        audioStream.getTracks().forEach(track => track.stop());\r\n      }\r\n    };\r\n  \r\n    return {\r\n      startRecording,\r\n      stopRecording,\r\n      recordedAudioURL // URL of the recorded audio to play the recording\r\n    };\r\n  };\r\n  \r\n  export default useAudioRecorder;\r\n  "],"mappings":";AAAA,SAAgBA,QAAQ,QAAQ,OAAO;;AAEvC;AACA;AACA;AACA;AACA,MAAMC,gBAAgB,GAAGA,CAAA,KAAM;EAAAC,EAAA;EAC3B,MAAM,CAACC,WAAW,EAAEC,cAAc,CAAC,GAAGJ,QAAQ,CAAqB,IAAI,CAAC;EACxE,MAAM,CA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