从 chrome 录制时,录音不会在 safari 中播放

Audio recording does not play in safari when recorded form the chrome

提问人:Mitul Chavda 提问时间:10/19/2023 更新时间:10/19/2023 访问量:31

问:

问题是当我从 Safari 录制音频时,它在 Safari 中播放良好,而在 Chrome 中录制音频时,它在 Chrome 中播放良好 但是当我在Chrome中录制音频并在Safari中播放时,它会播放整个音频,一旦进度条一次播放整个音频,但不会发出任何声音。 当我在 Safari 中录制音频时,它在 Chrome 中播放得很好

我正在使用 react-media-recorder-2 进行录制

这是记录器的代码

sexport const AudioBarPlayer = forwardRef<IAudioBarRef, AudioBarProps>((props: AudioBarProps, ref) =>
{
  const src = props.src;
  const scrollable = props.scrollable;
  const onChangeDuration = props.onChangeDuration;
  const onStopPlaying = props.onStopPlaying;
  const onStartPlaying = props.onStartPlaying;
  const cursorColor = props.cursorColor;
  const progressColor = props.progressColor;
  const waveColor = props.waveColor;
  const audioElementRef = useRef(new Audio());
  const containerRef = useRef<HTMLDivElement>(null);
  const waveSurferRef = useRef<WaveSurfer>();
  audioElementRef.current.controls = true;

  useEffect(() =>
  {
    waveSurferRef.current = WaveSurfer.create({
      cursorColor: cursorColor,
      progressColor: progressColor,
      // @ts-ignore
      container: containerRef.current,
      barWidth: 2,
      barRadius: 1,
      barGap: 3,
      waveColor: waveColor,
      cursorWidth: 2,
      height: 30,
      barHeight: scrollable ? 3 : 1,
      hideScrollbar: true,
      media: audioElementRef.current,
      duration: props.durationMs && (props.durationMs / 1000)
    });

    return () =>
    {
      waveSurferRef.current?.destroy();
    };
  }, []);

  useEffect(() =>
  {
    if(src)
    {
      waveSurferRef.current?.load(src).then(() =>
      {
        if(waveSurferRef.current)
        {
          onChangeDuration(props.durationMs
            ? (props.durationMs / 1000)
            : parseInt(waveSurferRef.current.getDuration().toString()));
        }
        waveSurferRef.current?.on("seeking", (args) =>
        {
          onChangeDuration(parseInt(args.toString()));
        });
        waveSurferRef.current?.on("finish", () =>
        {
          waveSurferRef.current?.seekTo(0);
          onStopPlaying();
          onChangeDuration(undefined);
        });
        waveSurferRef.current?.on("audioprocess", (args) =>
        {
          onChangeDuration(parseInt(args.toString()));
        });
        waveSurferRef.current?.on("play", () => onStartPlaying(waveSurferRef.current?.getCurrentTime() || 0));
        waveSurferRef.current?.on("pause", onStopPlaying);
        waveSurferRef.current?.on("destroy", onStopPlaying);
      });
    }
  }, [src]);

  useImperativeHandle(ref, () =>
  {
    return {
      play: () => waveSurferRef.current?.play().catch(() =>
      {
        if(src)
        {
          axios.get(src, {
              responseType: "blob"
            }
          ).then((response) =>
          {
            const blob = new Blob([response.data], {type: "audio/mp3; codecs=opus"});
            console.log("===response.data", response.data);
            waveSurferRef.current?.loadBlob(blob).then(() => waveSurferRef.current?.play());
          });
        }
      }),
      pause: () => waveSurferRef.current?.pause()
    };
  }, [waveSurferRef.current]);

  return (
    <div
      id="AudioBar"
      style={{
        width: "100%"
      }}
      ref={containerRef}
    />
  );
});

我希望当我在 Chrome 中录制它时,它也应该在 Safari 中播放'

JavaScript ReactJS Google-Chrome 音频 Safari

评论

0赞 Jaromanda X 10/19/2023
audio/mp3; codecs=opus这就像在你的特斯拉里放柴油吗?
0赞 Mitul Chavda 10/19/2023
所以任何建议我应该怎么做(我删除仍然不起作用)
0赞 Mitul Chavda 10/20/2023
我还发现将内容标题类型设置为音频/mpeg,但是当我从 safari 发送音频时,它播放得很好,所以我认为这不是问题。如果您有任何其他问题,请提供帮助

答: 暂无答案