Minim NullPointerException after multiple player.play() executions

edited December 2015 in Library Questions

The following code gives me a NullPointerException

// file: Lily.pde

import ddf.minim.*;
AudioPlayer player;
Minim minim; //audio context



// List of animals
StringList animalList = new StringList(
      "alligator", "bear", "cat", "dog", "elephant",
      "frog", "goat", "horse", "jay",
      "koala", "lion", "monkey", "newt", "owl",
      "pig", "raccoon", "squirrel", "turkey", "vulture",
      "whale", "yak", "zebra");

// animal image, sound and name matricies 
PImage[] images = new PImage[animalList.size()];
String[] animalSound = new String[animalList.size()]; 
String[] animalName = new String[animalList.size()];

String aList = "";
String aName = "";
PImage lily;
String imgName;
String sound;
int col;

void setup() {
  size(1024, 960);
  textSize(128);
 imageMode(CENTER);

  // Create and load image & sound matricies
  // ---------------------------------------
  int i = 0;
  for (String animal : animalList) {
    imgName = "data/" + animal + ".jpg";
    images[i] = requestImage(imgName);
    //images[i].resize(width,height);
    sound = "data/" + animal + ".mp3";
    animalSound[i] = sound;
    aName = "data/say-" + animal + ".mp3";
    animalName[i] = aName;
    aList += animal.charAt(0);
    i ++;
  }

  minim = new Minim(this);

  // Load start image and sound
  // --------------------------
  showLily();

    }

void draw() {

}

void keyPressed(){
 if(player.isPlaying()) {
    player.pause();
  }

  col = color(random(255), random(255),random(255));
  background(col);
  int key1 = aList.indexOf(key);
  if (key1 == -1) {
    background(col);
    fill(#18ED76);
    text(Character.toUpperCase(key), width/2, height/2);
  return; }

  playName(key1);

  showImage(key1);

  playSound(key1);

  char letter = aList.charAt(key1);
  showLetter(letter, animalList.get(key1));
}

void playSound(int k) {
  player = minim.loadFile(animalSound[k]);
  player.play();                                              <<- null pointer occurs here or below
}

void playName(int k) {
 player = minim.loadFile(animalName[k]);
 player.play();                                                 <<- null pointer also occurs here
}

void showLetter(char letter, String animal) {
  col = color(random(255), random(255),random(255));
  fill(col);
  letter = Character.toUpperCase(letter);
  textAlign(LEFT);
  text(letter, 50, 110);
  textAlign(CENTER);
  text(animal, width/2 - (animal.length()/2), height -75);
  textAlign(RIGHT);
  text(letter, width-100, height-50);
}

void showImage(int k) {
 image(images[k], width/2, height/2, 700,600);
}

void showLily() {
  col = color(random(255), random(255),random(255));
  background(col);
  player=minim.loadFile("data/kookaburra.mp3");
  player.play();
  imgName = ("data/Lily1.jpg");
  lily = loadImage(imgName);
  image(lily, width/2, height/2);
  fill(255);
  showLetter('L', "Lily");
}

void stop() {
 player.close();
 minim.stop();
 super.stop();
}

I get the NullPointerException at the places indicated above. I have tried placing

if(player.isPlaying()) {
    player.pause();
}

at the top of both the playSound() and playName() functions but neither has the effect I expect which is to let the next sound proceed. NullExceptionPointers do not occur after every new keystroke but as I increase the speed after about 5 or 6 keys I will get the exception. This application is for my 18 month old grand-daughter so of course she will "pound" the keyboard. Anyone have any ideas. I am using Processing 3.01 with the Minim imbeded library.

Answers

  • Answer ✓
    • draw() is the right place to draw!
    • Other callbacks like keyPressed() should be kept to a minimum code.
    • Mostly to just flag which key or mouse button had been hit.
  • ****GoToLoop Thank you for the quick response. Since I am a novice I'm going to think a little bit before responding with a question/comment. Ron

  • edited December 2015

    Declare a global variable: boolean keyHit;
    Inside keyPressed(): keyHit = true;
    Inside draw(), have this if () {} block:

    if (keyHit) {
      keyHit = false;
    
      // Place the rest of the code which was in keyPressed() below:
    }
    
  • GoToLoop I've placed all the former keyPressed() code into draw(). keyPressed() now only contains the loop() function. Draw ends with noLoop() function. I still have the same NullPointerException, but also get a new error:

    Couldn't open the line: line with format PCM_SIGNED 44100.0 Hz, 
    16 bit,    stereo, 4 bytes/frame, little-endian not supported.
    

    That is reported here

    (https://github.com/ddf/Minim/issues/45).
    

    There a few other issues but I think I can figure them out. Thanks for

  • GoToLoop, Looks like I missed your 12:37 edit. I'll strip the loop() and noLoop() functions and use your keyHit code. Thanks again, Ron

  • Answer ✓

    You can also issue noLoop() within setup().
    Then issue redraw() within keyPressed() after keyHit = true;

  • GoToLoop (I love this handle); All your suggestions have really helped me and have improved my code. However, I still get the same PCM_SIGNED error after so many player.play() executions. I am using Minim since it was included in Processing 3.01 and because Sound gives me an error which is also documented on the web. So my questions is if you have a preferred sound library that I might try? Thanks again.

  • I don't have much experience w/ Minim much less Sound libraries.
    I was just trying to help w/ the basic logic of your sketch rather than deal w/ the library.
    Nonetheless, you could post your most current attempt so others can help ya out too.

  • You definitely improved the logic!

Sign In or Register to comment.