Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Minim NullPointerException after multiple player.play() executions

$
0
0

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.


Viewing all articles
Browse latest Browse all 2896

Trending Articles