最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firefox recording blank audio on mobile - Stack Overflow

programmeradmin1浏览0评论

I've ran into a strange bug with Firefox on mobile where the default media recorder output ogg data of correct length but with no sound.

The code I'm using is essentially copy-pasted from the MDN docs, minus a few state management bits that should not be consequential:

  const startRecording = async () => {
    try {
      set_button_is_loading(true);
      const stream = await window.navigator.mediaDevices.getUserMedia({ audio: true, video: false });
      setAudioStream(stream);
        const mediaRecorder = new MediaRecorder(stream);        
        mediaRecorderRef.current = mediaRecorder;      
        const chunks: BlobPart[] = [];

      mediaRecorder.ondataavailable = (e) => {
        if (e.data.size > 0) {
          if (e.data.type) {
            mime_type.current = e.data.type
          }
          chunks.push(e.data);
        }
      };

      mediaRecorder.onstop = async () => {
        try {
          const blob = new Blob(chunks, { type: mime_type.current });
          let answer = await api_stt(blob, mime_type.current);
          let prev_answer = current_answer;
          if (!prev_answer) {
            prev_answer = "";
          } else if (prev_answer.length > 3) {
            prev_answer = prev_answer + "\n";
          }
          set_current_answer(prev_answer + answer);
          set_button_is_loading(false);
        } catch (e) {
          console.error(e)
          set_global_error("Error transcribing audio, please speak clearly.")
          set_button_is_loading(false);
        }
      };

      mediaRecorder.start();
      setIsRecording(true);
      setTimeout(() => {
        set_button_is_loading(false);
      }, 500)
    } catch (err) {
      console.error("Error accessing microphone:", err);
    }
  };

  const stopRecording = () => {
    if (mediaRecorderRef.current && audioStream) {
      set_button_is_loading(true);
      setTimeout(() => {
        if (mediaRecorderRef.current && audioStream) {
          mediaRecorderRef.current.requestData();
          mediaRecorderRef.current!.stop();
          audioStream.getTracks().forEach((track) => track.stop());
          setAudioStream(null);
          setIsRecording(false);
        }
      }, 500)
    }
  };

It works on all browsers except firewfox mobile (all is a stretch, but ff, chrome and safari desktop as well as chrome and safari mobile both android and iOS)

I've not figured out a way to "unmuted" the audio stream, the browser reports recording just fine and this is an issue that I've noticed across various web audio-recording websites i.e. I assume it might be a recent breaking change that affects a bunch of codebases.

发布评论

评论列表(0)

  1. 暂无评论