通过带有Minim的get.LineIn记录样本时出现空指针异常

时间:2016-04-01 14:36:43

标签: java processing

目前我正在使用Sparkfun Guide to Processing学习处理。到目前为止好书!但现在我仍然坚持第11章的教程:使用Minim库录制音频。

使用下面的代码,正如书中所解释的,当我尝试通过向左或向右按​​下来记录样本时,我得到一个空指针异常。我很想知道这里有什么问题。我无法解决问题。我确信这是非常简单的事情,但我看不到它。本章下载了一些示例代码,但不包括此特定草图。

感谢您的帮助!

这是空指针代码(编辑后包含一些我之前没有包含的行,因为它们似乎没有导致草图崩溃):

==== JavaSound Minim Error ====
==== Error obtaining new output stream:/Users/*edited full path*/soundboard/data/right.wav (No such file or directory)

==== JavaSound Minim Error ====
==== Error obtaining new output stream: /Users/*edited full path*/soundboard/data/left.wav (No such file or directory)

java.lang.NullPointerException

at ddf.minim.javasound.JSStreamingSampleRecorder.samples(JSStreamingSampleRecorder.java:177)
at ddf.minim.SignalSplitter.samples(SignalSplitter.java:112)
at ddf.minim.javasound.JSAudioOutput.run(JSAudioOutput.java:84)

这是我用于草图的代码:

import ddf.minim.*;

Minim minim;
AudioInput mic;
AudioRecorder left;
AudioRecorder right;
AudioSample Pright;
AudioSample Pleft;

void setup(){
  size(800,900);
  minim = new Minim(this);
  mic = minim.getLineIn();
  right = minim.createRecorder(mic,"data/right.wav");
  left = minim.createRecorder(mic, "data/left.wav");
};

void draw(){

};

void keyPressed(){

  //begin left record
  if(keyCode == LEFT){
    if(left.isRecording()){
      left.endRecord();
      left.save();
    }
    else{
      left.beginRecord();
    };
  };

  //begin right record
  if(keyCode == RIGHT){
    if(right.isRecording()){
      right.endRecord();
      right.save();
    }
    else{
      right.beginRecord();
    };
  };

  //play the right sample
  if(keyCode == UP){
    Pright = minim.loadSample("right.wav");
    Pright.trigger();
  };

  //play the left sample
  if(keyCode == DOWN){
    Pleft = minim.loadSample("left.wav");
    Pleft.trigger();
  };

};

1 个答案:

答案 0 :(得分:0)

好的,经过一番摆弄,我设法解决了这个问题。感谢您对我的问题的评论中的提示,他们促使我进一步调查。

在书中,它要求您将其作为新草图启动。但是,当您开始新草图时,Processing不会自动创建“数据”文件夹。只有在通过Sketch>向草图添加文件时才会创建数据文件夹。添加文件。

void setup(){
  size(800,900);
  minim = new Minim(this);
  mic = minim.getLineIn();
  right = minim.createRecorder(mic,"data/right.wav");
  left = minim.createRecorder(mic, "data/left.wav");
};

在上面的代码中,'right'和'left'对象无处可以保存.wav文件。我手动添加了一个数据文件夹,嘿-presto,它可以工作。

一旦我开始工作,就出现了另一个错误:

  //play the right sample
  if(keyCode == UP){
    Pright = minim.loadSample("right.wav");
    Pright.trigger();
  };

  //play the left sample
  if(keyCode == DOWN){
    Pleft = minim.loadSample("left.wav");
    Pleft.trigger();
  };

如果您在之前按下向上或向下,则表示您已录制样本,然后程序崩溃,因为没有可播放的文件。添加一个布尔值使我能够编写一个更具体的“if”语句,导致程序只在尝试录制文件时才尝试播放该文件。

请参阅下面的完整代码。我希望通过这本特定书籍学习处理的其他人觉得这很有帮助。非常感谢你的帮助。

import ddf.minim.*;

Minim minim;
AudioInput mic;
AudioRecorder left;
AudioRecorder right;
AudioSample Pright;
AudioSample Pleft;
boolean leftRecorded;
boolean rightRecorded;

void setup(){
  size(800,900);
  minim = new Minim(this);
  mic = minim.getLineIn();
  right = minim.createRecorder(mic,"data/right.wav");
  left = minim.createRecorder(mic, "data/left.wav");
};

void draw(){
;
};

void keyPressed(){

  //begin left record
  if(keyCode == LEFT){
    if(left.isRecording()){
      left.endRecord();
      left.save();
      leftRecorded = true;
    }
    else{
      left.beginRecord();
    };
  };

  //begin right record
  if(keyCode == RIGHT){
    if(right.isRecording()){
      right.endRecord();
      right.save();
      rightRecorded = true;
    }
    else{
      right.beginRecord();
    };
  };

  //play the right sample
  if(keyCode == UP && rightRecorded){
    Pright = minim.loadSample("right.wav");
    Pright.trigger();
  };

  //play the left sample
  if(keyCode == DOWN && leftRecorded){
    Pleft = minim.loadSample("left.wav");
    Pleft.trigger();
  };

};