Processing minim null pointer

2019-07-21 01:21发布

问题:

I am using processing with Minim, but I can't figure out how to play files, if the minim loading files are in another class. I used both AudioSnippet and AudioSample and they both give me NullPointerException. Here is a the PlayAudio class.

  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();
  }
}

The other is a standard processing setup() and draw(). Files should be played when mouse is pressed.

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();
   }
}

The errors I get are:

==== 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:

Just put minim = new Minim( this ); into setup block. If you do this inside PlayAudio class you get wrong argument for this.

Working code:

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();
  }
}