处理minim空指针

时间:2013-12-28 15:09:32

标签: java processing minim

我正在使用Minim进行处理,但如果minim加载文件在另一个类中,我无法弄清楚如何播放文件。我同时使用了AudioSnippetAudioSample,他们都给了我NullPointerException。这是PlayAudio类。

  Minim minim;
  AudioSample sample;
  AudioSnippet snippet;

class PlayAudio {

  PlayAudio() {
    minim = new Minim(this);

    sample = minim.loadSample("Audio/hover1.mp3", 2048);
    snippet = minim.loadSnippet("Audio/hover1.mp3");
  }

  void playSnippet() {
    sample.trigger();
    snippet.play();
  }
}

另一种是标准处理setup()draw()。按下鼠标时应播放文件。

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

PlayAudio audio = new PlayAudio();

void setup() {
  size(300, 300);
  background(0);
}

void draw() {
   if(mousePressed) {
    audio.playSnippet();
   }
}

我得到的错误是:

==== JavaSound Minim Error ====
==== Couldn't find a sketchPath method on the file loading object provided!
==== File recording will be disabled.

==== JavaSound Minim Error ====
==== Couldn't find a createInput method in the file loading object provided!
==== File loading will be disabled.

==== JavaSound Minim Error ====
==== Error invoking createInput on the file loader object: null

=== Minim Error ===
=== Couldn't load the file Audio/hover1.mp3

1 个答案:

答案 0 :(得分:2)

只需将 minim = new Minim( this ); 放入设置块即可。如果你在PlayAudio课程中这样做,你会得到 this 的错误论据。

工作代码:

import ddf.minim.*;

Minim minim;
AudioSample sample;
AudioSnippet snippet;

PlayAudio audio;

void setup() {
  size(300, 300);
  minim = new Minim(this);
  audio = new PlayAudio();
  background(0);
}

void draw() {
  if (mousePressed) {
    audio.playSnippet();
  }
}

class PlayAudio {
  PlayAudio() {
    sample = minim.loadSample("Audio/hover1.mp3", 2048);
    snippet = minim.loadSnippet("Audio/hover1.mp3");
  }

  void playSnippet() {
    sample.trigger();
    snippet.play();
  }
}