Angular和p5.js - p5.loadSound不是函数

时间:2017-07-28 19:16:00

标签: angular typescript p5.js

我想在Angular项目中使用p5.js时遇到问题。

我使用Angular CLI。 在我的 .angular-cli.json 文件中包含p5.js:

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

然后在我的组件中写道:

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;

  }

}

然后当我想加载我的歌并使用 loadSound()时,JS告诉我 错误TypeError:p.loadSound不是函数

好的,JS在我的文件中找不到这个方法,但是在浏览器控制台中我可以这样写:

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

一切正常(screenshot of my console)!

我试着在我的组件中写道:

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

它不起作用。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

替换

import * as p5 from 'p5';

*

declare var p5: any;

答案 1 :(得分:-1)

我不知道为什么,但对我来说,在调用 p5sound 之前记录 loadSound 使它起作用。

与:

import * as p5 from 'p5';
import * as p5sound from 'p5/lib/addons/p5.sound';

和:

console.log(p5sound)
this.song = s.loadSound('assets/samples/CpW_WetHitE-01.wav');