2

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
4

1 回答 1

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();
  }
}
于 2014-05-24T15:34:56.730 回答