Threading with minim

edited November 2013 in Questions about Code

Hi! I'm trying to figure out ways to play 2 snippets at once, and ended up using threads. The music is playing but I'm getting a null pointer exception. Can any of you figure it out?

Code---

import ddf.minim.*;

Minim minim;
AudioSnippet sound1;
AudioSnippet sound2;

void setup()
{
thread("Ape");
thread("Bjorn");

     minim = new Minim(this);
   sound2 =minim.loadSnippet("mysong.mp3");
   sound1= minim.loadSnippet("tast.mp3");

}

void draw()
{

  if(!a){
  Ape();
  Bjorn();
  a=true;}

}

void Ape(){
  sound1.loop();


}
void Bjorn(){
  sound2.loop();

}

void stop()
{
minim.stop();
 sound1.close();
 sound2.close();
 super.stop();
}

Error-----

java.lang.NullPointerException
    at yep.Bjorn(yep.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at processing.core.PApplet.method(PApplet.java:4041)
    at processing.core.PApplet$4.run(PApplet.java:4071)
java.lang.NullPointerException
    at yep.Ape(yep.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at processing.core.PApplet.method(PApplet.java:4041)
    at processing.core.PApplet$4.run(PApplet.java:4071)
Tagged:

Answers

  • Answer ✓

    To start, you're starting the threads before the AudioSnippets that they need have been initialized! You'll need to do that after... I'm not even sure if Minim is designed to be thread-synchronized...

  • thanks! Is there any other libary you would recommend for playing sound at the same time?

  • I'm pretty sure that Minim is capable of playing multiple sounds at the same time... it's just that you don't need Threads to do that. Minim should take care of everything behind the scenes. What's wrong with telling both sounds to loop in setup()?

Sign In or Register to comment.