Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not get plugins work with wavesurfer.js using typescript

I am trying to generate a wave form for the audio and video content in my application, currently using angular 6 and TypeScript. I could make the wave form using the wavesurfer.js but i could not get the timeline plug in working. In my angular.json i have scripts like this

"scripts": [
          "node_modules/jquery/dist/jquery.min.js",  
          "node_modules/popper.js/dist/umd/popper.min.js",  
          "node_modules/bootstrap/dist/js/bootstrap.min.js",
          "node_modules/moment/min/moment.min.js",
          "node_modules/wavesurfer.js/dist/wavesurfer.js",
          "node_modules/wavesurfer.js/dist/plugin/wavesurfer.timeline.js" 
        ]

and in my component.ts file i have code like

declare var WaveSurfer;
declare var TimelinePlugin;

export class TranscriptComponent implements OnInit {

 ngAfterViewInit() {
  this.wavesurfer = WaveSurfer.create({
   container: '#waveform',
   waveColor: 'violet',
   progressColor: 'purple',
   cursorColor: '#d9fb36',
   normalize: true,
   skipLength: 15,
   hideScrollbar: true,
   backend: 'MediaElement',
   plugins: [
     TimelinePlugin.create({
       // plugin options ...
     })
   ]
 });

}

with-out the plugin i could get the waveform, but if i try to add the plugins, i get error "TimelinePlugin is undefined." Can any tell me how to use these plugins using typescript. An Example would be great.

like image 355
Manoj Avatar asked Oct 23 '25 18:10

Manoj


1 Answers

I got this working (after some trial-and-error) by explicitly adding the path to the plugin I needed to the 'scripts' arrays (architect and test) in angular.json:

"scripts": [
    "./node_modules/wavesurfer.js/dist/wavesurfer.js",
    "./node_modules/wavesurfer.js/dist/plugin/wavesurfer.regions.js"
],

I then imported the plugin into the component with the audio player:

import * as WaveSurfer from 'wavesurfer.js';
import * as WaveSurferRegions from 'wavesurfer.js/dist/plugin/wavesurfer.regions.js';

And initialised the player:

ws: any;

ngOnInit() {
    this.ws = WaveSurfer.create({
        container: document.querySelector('#player_waveform'),
        backend: 'MediaElement',
        plugins: [
            WaveSurferRegions.create({
                regions: [
                    {
                        start: 1,
                        end: 3,
                        color: 'hsla(400, 100%, 30%, 0.5)'
                    }, {
                        start: 5,
                        end: 7,
                        color: 'hsla(200, 50%, 70%, 0.4)'
                    }
                ]
            })
        ]
    });
}

I also made some changes to npm but hard to say whether this is what got things working. I did have both the newer wavesurfer.js and wavesurfer installed. I removed wavesurfer, which might have helped?

like image 150
Matt Saunders Avatar answered Oct 25 '25 07:10

Matt Saunders