Giter VIP home page Giter VIP logo

Comments (4)

ShiiRochi avatar ShiiRochi commented on May 24, 2024 1

@bramses, hi, I tried to reproduce it and succeeded.

I will place working code below, but it should be mentioned that it's a little bit tricky.

import type { NextPage } from 'next'
import Head from 'next/head'
import Image from 'next/image'
import dynamic from "next/dynamic";
import styles from '../styles/Home.module.css'
import {useCallback, useEffect, useState} from "react";

const WaveSurfer = dynamic(() => import("wavesurfer-react").then(m => m.WaveSurfer), { ssr: false });

const Home: NextPage = () => {
  const [plugins, setPlugins] = useState([]);

  const onMount = useCallback((ws$) => {
    console.log({ ws$ });
    if (!ws$) return;
    ws$.load("/record_1.mp3");
  }, []);

  useEffect(() => {
    if (typeof window === "undefined") return;
    /*
     dynamic("...") with wavesurfer plugins doesn't work
     because next will surround it with LoadableComponent
    */
    import("wavesurfer.js/dist/plugin/wavesurfer.timeline").then((TimelinePlugin) => {
      setPlugins([{
        plugin: TimelinePlugin.default,
        options: {
          container: "#timeline"
        }
      }]);
    });
  }, [])

  return (
    <div className={styles.container}>
      <main className={styles.main}>
        <h1 className={styles.title}>
          Welcome to <a href="https://nextjs.org">Next.js!</a>
        </h1>
        <div style={{ width: "100%" }}>
          {/*
          We cannot you dynamic with WaveForm as far as dynamic surrounds component with LoadableComponent,
          that makes it impossible to read container from WaveForm's props.
          */}
          <WaveSurfer onMount={onMount} container="#wavesurfer" plugins={plugins}>
            <div id="wavesurfer"/>
            <div id="timeline" />
          </WaveSurfer>
        </div>
      </main>
    </div>
  )
}

export default Home

from wavesurfer-react.

bramses avatar bramses commented on May 24, 2024 1

@ShiiRochi it works, thank you!

Screen Shot 2022-07-16 at 7 32 08 PM

In the interim, I also was able to figure out how to run vanilla WaveSurfer in Next, if it helps w/ future releases!

const formWaveSurferOptions = (ref) => ({
  container: ref,
  waveColor: "#eee",
  progressColor: "#0178FF",
  cursorColor: "OrangeRed",
  barWidth: 3,
  barRadius: 3,
  responsive: true,
  height: 150,
  normalize: true,
  partialRender: true,
  plugins: [],
});

const url =
    "https://www.mfiles.co.uk/mp3-downloads/brahms-st-anthony-chorale-theme-two-pianos.mp3";

  useEffect(() => {
    const create = async () => {
      const WaveSurfer = (await import("wavesurfer.js")).default;
      const MarkersPlugin = (await import("wavesurfer.js/src/plugin/markers"))
        .default;

      const options = formWaveSurferOptions(waveformRef.current);
      options.plugins.push(
        MarkersPlugin.create({
          markers: [
            {
              time: 58,
              label: "",
              color: "#ff990a",
            },
            {
              time: 5.5,
              label: "",
              color: "#ff990a",
            },

            {
              time: 24,
              label: "END",
              color: "#00ffcc",
              position: "top",
            },
          ],
        })
      );

      wavesurfer.current = WaveSurfer.create(options);
      wavesurfer.current.load(url);

      wavesurfer.current.on("marker-click", function (marker) {
        console.log("marker drop", marker);
      });

      wavesurfer.current.on("audioprocess", function () {
        const currentTime = wavesurfer.current.getCurrentTime();
        setProgress(currentTime);
      });
    };

    create();

    return () => {
      if (wavesurfer.current) {
        console.log("destroy");
        wavesurfer.current.destroy();
      }
    };
  }, []);

const handlePlayPause = () => {
    setPlaying(!playing);
    wavesurfer.current.playPause();
  };

  const back30 = () => {
    wavesurfer.current.skipBackward(30);
  };

const forward30 = () => {
    wavesurfer.current.skipForward(30);
  };
  
<div id="waveform" ref={waveformRef} />
      <div className="controls">
        <div onClick={back30}>Back 30</div>
        <div onClick={handlePlayPause}>{!playing ? "Play" : "Pause"}</div>
        <div onClick={forward30}>Forward 30</div>
      </div>

from wavesurfer-react.

ShiiRochi avatar ShiiRochi commented on May 24, 2024

@anyasinyavskaya, hello!

  1. What version of wavesurfer.js you're using?
  2. Please, see wavesurfer.js#2359, maybe in wavesurfer.js package repo you will get an answer, also there is a duplicate link at the end that relates to jest somehow.

Some precautions:

  1. if you're using Next, try to surround wavesurfer-react components with dynamic call with ssr set to false;
  2. if you're using other SSR stuff, try to surround wavesurfer-react components with client-only parent components that will render components only in browser environment.

from wavesurfer-react.

bramses avatar bramses commented on May 24, 2024

Running into the same issue (in NextJS)

import dynamic from "next/dynamic";
const WaveSurfer = dynamic(() => import("wavesurfer-react"), {
  ssr: false,
});

ReferenceError: self is not defined...node_modules/wavesurfer.js/dist/plugin/wavesurfer.regions.min.js (6:255)

from wavesurfer-react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.