Angular and p5.js - p5.loadSound is not a function

。_饼干妹妹 提交于 2019-12-10 10:32:00

问题


I have a problem when want to use p5.js in my Angular project.

I use Angular CLI. Include p5.js in my .angular-cli.json file:

"scripts": [    
  "../node_modules/p5/lib/p5.min.js",   
  "../node_modules/p5/lib/addons/p5.sound.js",   
  "../node_modules/p5/lib/addons/p5.dom.js"  
],

Then in my component I write:

import { Component, OnInit } from '@angular/core';

import * as p5 from 'p5';

@Component({
  selector: 'app-player',
  templateUrl: './player.component.html',
  styleUrls: ['./player.component.scss']
})
export class PlayerComponent implements OnInit {

  play: boolean = false;

  constructor() { }

  ngOnInit() {
    const s = (p) => {

      let song;
      let canvas;

      p.preload = () => {
        console.log('preload');
        song = p.loadSound('assets/music/Thunderstruck.mp3');
      }

      p.setup = () => {
        canvas = p.createCanvas(595, 100);
        canvas.parent('equalizer');
        p.background(0);
      }
    }

    let player = new p5(s);

  }

  onPlay() {
    this.play = !this.play;

  }

}

And then when I want load my song and use loadSound(), JS tells me ERROR TypeError: p.loadSound is not a function

Ok, JS can't find this method in my file, but in browser console I can write something like this:

let song = p5.prototype.loadSound('assets/music/Thunderstruck.mp3');
song.play();

and everything works (screenshot of my console)!

I tried to write in my component:

song = p.prototype.loadSound('assets/music/Thunderstruck.mp3');

It doesn't work.

What am I doing wrong?


回答1:


Replace

import * as p5 from 'p5';

*

declare var p5: any;


来源:https://stackoverflow.com/questions/45380883/angular-and-p5-js-p5-loadsound-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!