I just (finally) just tried to load sound files before I need them but there are some problems. Well, one actually ^^
I have to create my variables like this at the beginning SamplePlayer music1
and when I need them I do
try
{
music1 = new SamplePlayer(ac, new Sample(sketchPath("")));
}
catch(Exception e)
{
e.printStackTrace();
exit();
}
It works but the problem is to stop them, because in my program check when I am in the menu (for example) the music I need and thus the one I need to pause.
if (/*condition*/)
{
music0.pause(false);
music1.pause(true);
music1.reset();
}
So i get a NullPointerException, wich is normal because the music is load after :/
Of course it will. You're putting it before the try/catch, which probably means that the music file never got loaded. You should load all files in setup if possible (I'm missing something here, right?)
it was stupid to use them before loading them, but I actually don't have the choice
You definitely always have a choice. For example, you could:
set boolean musicLoaded -- initially false, and true once you load your sample --
and only execute your control code (e.g. in your menu) if musicLoaded==true -- otherwise, disable those controls.
create a dummy SamplePlayer that you assign during setup music1 = new SamplePlayer(ac, new Sample(sketchPath("zero-second-recording.mp3"))); When you are ready to load something, replace it.
There are more options -- those are just two of them.
I knew there was possibilities but I couldn't figure them out, the first one won't work for reasons but the second seems pretty interesting. I'm trying it right now
Answers
Hey there again :)
I just (finally) just tried to load sound files before I need them but there are some problems. Well, one actually ^^
I have to create my variables like this at the beginning
SamplePlayer music1
and when I need them I do
It works but the problem is to stop them, because in my program check when I am in the menu (for example) the music I need and thus the one I need to pause.
So i get a NullPointerException, wich is normal because the music is load after :/
Maybe that's because you're using exit(). Perhaps System.exit(-1) may work? (-1 to indicate error)
No, there are no issues with the try/catch, only with the ìf()`wich have to occur before the try/catch but gives me a NullPointerException
Of course it will. You're putting it before the try/catch, which probably means that the music file never got loaded. You should load all files in setup if possible (I'm missing something here, right?)
Loading the files in my setup will cause my first problem, which was the time to load all my files :/
And yes I know it was stupid to use them before loading them, but I actually don't have the choice ^^
Please format the code by editing your original post: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text
re:
You definitely always have a choice. For example, you could:
boolean musicLoaded
-- initially false, and true once you load your sample -- and only execute your control code (e.g. in your menu) ifmusicLoaded==true
-- otherwise, disable those controls.music1 = new SamplePlayer(ac, new Sample(sketchPath("zero-second-recording.mp3")));
When you are ready to load something, replace it.There are more options -- those are just two of them.
Thanks :)
I knew there was possibilities but I couldn't figure them out, the first one won't work for reasons but the second seems pretty interesting. I'm trying it right now