Comments (4)
@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.
@ShiiRochi it works, thank you!
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.
@anyasinyavskaya, hello!
- What version of wavesurfer.js you're using?
- 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:
- if you're using Next, try to surround
wavesurfer-react
components with dynamic call withssr
set tofalse
; - 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.
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)
- Waveform customisation HOT 2
- Can not get zoom to work HOT 5
- Cannot use import statement outside a module HOT 7
- recreate timeline if its id is changed HOT 1
- "Cannot use import statement outside a module" with Next.js HOT 3
- getCurrentTime without triggering too much re-render on the Component HOT 2
- Is it possible to use this with Tauri / NextJS SSG? HOT 2
- generating new markers in component wrapped by <StrictMode> not rendering new markers HOT 4
- wavesurfer-react with react-beautiful-dnd issue HOT 5
- Align the button- Should be at the bottom of each waveform HOT 1
- Support content for regions HOT 3
- Support for wavesurfer.js 7+ HOT 5
- wavesurfer-react: V3 HOT 18
- audio load issue HOT 2
- codesandbox code issue HOT 2
- add first-interaction github action
- improve demo webpage
- Minimap support HOT 1
- Issue with changing the style of the waveform scrollbar HOT 1
- How to get the list of all regions? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wavesurfer-react.