Processing minim null pointer

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 02:34:29

问题


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


来源:https://stackoverflow.com/questions/20816031/processing-minim-null-pointer

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