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

Issue with Canvas Animation Not Working in Safari and iOS When Using M3U8 Audio Format - Stack Overflow

programmeradmin4浏览0评论

I am working on an audio player where a logo is drawn and animated using the element. The animation is based on the audio frequencies analyzed in real time.

Everything works as expected when using an MP3 audio source, but when switching to an M3U8 (HLS) format, the animation stops working specifically on iOS and Safari.

I am using hls.js to handle M3U8 playback, and the audio itself plays fine across all browsers, including Safari. However, the frequency data needed for animation is not being processed correctly on iOS and Safari.

const initializeAudio = () => {
    if (!audioRef.current) {
      audioRef.current = new Audio();
      audioRef.current.loop = false;
      audioRef.current.volume = volume;

      if (Hls.isSupported()) {
        const hls = new Hls();
        hls.loadSource(`.m3u8`);
        hls.attachMedia(audioRef.current);
      } else {
        audioRef.current.src =
          ".m3u8";
      }
    }

    if (!audioContextRef.current) {
      audioContextRef.current = new (window.AudioContext ||
        (window as unknown as { webkitAudioContext?: typeof AudioContext })
          .webkitAudioContext)();

      const analyser = audioContextRef.current.createAnalyser();
      analyser.fftSize = 256;
      analyserRef.current = analyser;

      const source = audioContextRef.current.createMediaElementSource(
        audioRef.current
      );

      const gainNode = audioContextRef.current.createGain();
      gainNode.gain.value = volume;
      gainNodeRef.current = gainNode;

      source.connect(gainNode);
      gainNode.connect(analyser);
      analyser.connect(audioContextRef.current.destination);
    }
  };
  • Verified that AudioContext and AnalyserNode are properly initialized.
  • Checked if createMediaElementSource(audioElement) works correctly with M3U8.
  • Ensured that Safari has autoplay permissions enabled.
  • Tried different configurations for the AnalyserNode (e.g., fftSize, smoothingTimeConstant).
发布评论

评论列表(0)

  1. 暂无评论