Giter VIP home page Giter VIP logo

Comments (7)

ShiiRochi avatar ShiiRochi commented on May 24, 2024 1

Hi, @Simoneth, check your dynamic import, according to the NextJS docs first argument has to be a function

NEXT | Dynamic Import

from wavesurfer-react.

ShiiRochi avatar ShiiRochi commented on May 24, 2024 1

@Simoneth, I've created next-project

{
    ...,
    "next": "13.1.0",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "wavesurfer-react": "2.2.2",
    "wavesurfer.js": "6.4.0",
    ...
}

Within the project, I implemented the following component:

import { useCallback, useMemo, useRef, useState } from 'react';
import { WaveSurfer, WaveForm, Region, Marker } from 'wavesurfer-react';
import RegionsPlugin from 'wavesurfer.js/dist/plugin/wavesurfer.regions.min';
import TimelinePlugin from 'wavesurfer.js/dist/plugin/wavesurfer.timeline.min';
import MarkersPlugin from 'wavesurfer.js/src/plugin/markers';

export default function Wavesurfer({ id }) {
  const waveformId = 'waveform' + '-' + id;
  const timelineId = `timeline-${id}`;

  const plugins = useMemo(() => {
    return [
      {
        plugin: RegionsPlugin,
        options: { dragSelection: true },
      },
      {
        plugin: TimelinePlugin,
        options: {
          container: timelineId,
        },
      },
      {
        plugin: MarkersPlugin,
        options: {
          markers: [{ draggable: true }],
        },
      },
    ];
  }, [timelineId]);

  const [regions, setRegions] = useState([
    {
      id: 'region-1',
      start: 0.5,
      end: 10,
      color: 'rgba(0, 0, 0, .5)',
      data: {
        systemRegionId: 31,
      },
    },
    {
      id: 'region-2',
      start: 5,
      end: 25,
      color: 'rgba(225, 195, 100, .5)',
      data: {
        systemRegionId: 32,
      },
    },
    {
      id: 'region-3',
      start: 15,
      end: 35,
      color: 'rgba(25, 95, 195, .5)',
      data: {
        systemRegionId: 33,
      },
    },
  ]);

  const [markers, setMarkers] = useState([
    {
      time: 5.5,
      label: 'V1',
      color: '#ff990a',
      draggable: true,
    },
    {
      time: 10,
      label: 'V2',
      color: '#00ffcc',
      position: 'top',
    },
  ]);

  const wavesurferRef = useRef(null);

  const onWaveSurferMount = useCallback((waveSurfer) => {
    if (waveSurfer.markers) {
      waveSurfer.clearMarkers();
    }

    wavesurferRef.current = waveSurfer;

    if (wavesurferRef.current) {
      wavesurferRef.current.load('/audios/1.mp3');

      wavesurferRef.current.on('ready', () => {
        console.log('WaveSurfer is ready');
      });

      wavesurferRef.current.on('region-removed', (region) => {
        console.log('region-removed --> ', region);
      });

      wavesurferRef.current.on('loading', (data) => {
        console.log('loading --> ', data);
      });
    }
  }, []);

  return (
    <WaveSurfer plugins={plugins} onMount={onWaveSurferMount}>
      <WaveForm id={waveformId}>
        {regions.map((regionProps) => (
          <Region
            onUpdateEnd={() => {}}
            key={regionProps.id}
            {...regionProps}
          />
        ))}
        {markers.map((marker, index) => {
          return (
            <Marker
              key={index}
              {...marker}
              onClick={(...args) => {
                console.log('onClick', ...args);
              }}
              onDrag={(...args) => {
                console.log('onDrag', ...args);
              }}
              onDrop={(...args) => {
                console.log('onDrop', ...args);
              }}
            />
          );
        })}
      </WaveForm>
      <div id={timelineId}></div>
    </WaveSurfer>
  );
}

At first run, I received the same error about container being not specified, but I found out that I made a mistake in plugins props. After the following change everything start to work:

  ...
  const plugins = useMemo(() => {
    return [
      {
        plugin: RegionsPlugin,
        options: { dragSelection: true },
      },
      {
        plugin: TimelinePlugin,
        options: {
          // old value was only timelineId
          container: '#' + timelineId,
        },
      },
      {
        plugin: MarkersPlugin,
        options: {
          markers: [{ draggable: true }],
        },
      },
    ];
  }, [timelineId]);
  ...

Thus, make sure that both id in plugins list and in div, that is considered to be a timeline container contain and receive (respectively) the same value taking into account that container in plugins should also be prefixed with "#". Currently timeline will not rebuild if some of its props would change.

from wavesurfer-react.

ShiiRochi avatar ShiiRochi commented on May 24, 2024 1

@Simoneth, ShiiRochi | Experimental Next Project

from wavesurfer-react.

Simoneth avatar Simoneth commented on May 24, 2024

Here my Wavesurfer component:

import { WaveSurfer, WaveForm, Region } from "wavesurfer-react";
import RegionsPlugin from "wavesurfer.js/dist/plugin/wavesurfer.regions.min";
import TimelinePlugin from "wavesurfer.js/dist/plugin/wavesurfer.timeline.min";

export default function Wavesurfer(props) {
    const [timelineVis] = useState(true);
    const minSec = 5;
    const maxSec = 30;
    const absoluteMaxRegions = 20;
    const [maxRegions, setMaxRegions] = useState();

    const plugins = useMemo(() => {
        return [
            {
                plugin: RegionsPlugin,
                options: { dragSelection: true }
            },
            timelineVis && {
                plugin: TimelinePlugin,
                options: {
                    container: "#timeline" + props.id
                }
            }
        ].filter(Boolean);
    }, [timelineVis]);
    
    return (
        <WaveSurfer plugins={plugins} onMount={handleWSMount}>
            <WaveForm
                id={"waveform" + props.id}
                cursorColor="OrangeRed"
                progressColor="#00a3ff"
                barWidth="3"
                barRadius="3"
                responsive="true"
                partialRender="true"
            >
                {props.regions.map((regionProps) => (
                    <Region
                        onUpdateEnd={handleRegionUpdate}
                        key={regionProps.id}
                        minLength={minSec}
                        maxLength={maxSec}
                        maxRegions={maxRegions}
                        {...regionProps}
                    />
                ))}
            </WaveForm>

            <div className="timeline" id={"timeline" + props.id} />
        </WaveSurfer>
    )

And how I call it:

import dynamic from 'next/dynamic';
const Wavesurfer = dynamic(import('src/components/wavesurfer'), { ssr: false })

from wavesurfer-react.

Simoneth avatar Simoneth commented on May 24, 2024

Thanks,
So, I changed the import to this:

const Wavesurfer = dynamic(() => import('src/components/wavesurfer'), {
  ssr: false,
})

The previous error has disappeared, but it doesn't solve the general problem for me:

Screenshot 2023-01-23 at 11 14 12

from wavesurfer-react.

Simoneth avatar Simoneth commented on May 24, 2024

Sorry again for the inconvenience but could you give me a link to see your example...that would really help, because I still can't figure it out.

Thanks!

from wavesurfer-react.

Simoneth avatar Simoneth commented on May 24, 2024

Thanks,
I finally figured out that mine was a re-rendering issue (my id was auto-generated dynamically via props).

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.