We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I am trying to do something but I got an error. What I wanna do is to add a default sound to a custom class that will be played each time I click on it. So I created a new object :
Minim minim;
AudioPlayer globalsound;
void setup()
{
size(1366,768);
globalsound= minim.loadFile("sound.mp3");
}
void draw()
{
//do things
}
class Mynewobject
{
AudioPlayer defaultsound;
Mynewobject()
{
this.defaultsound=globalsound;
this.defaultsound.play();
}
}
Can you please help me to find out what I'm doing wrong ? Thanks in advance !
Answers
To help people in the forum read and run your code and give feedback, please format your code -- edit your post, highight the code, and press CTRL+o.
Thank you, I have done this, I hope it will help !
Please edit your post and format your code. Select your code and press ctrl+o and make sure you have an empty line above and below your code.
You need to call play() or loop() in setup for the file to be executed. On the side, if your class is going to control the AudioPlayer object, why not to include this AudioPlayer object in your class? Then you need to create an object of your class and you could call associated functions to interact with your AudioPlayer object, like I show below.
Please note the code below was not tested but to demonstrate a concept.
Check minim documentation: http://code.compartmental.net/tools/minim/
http://code.compartmental.net/tools/minim/quickstart/
http://code.compartmental.net/minim/javadoc/
Kf
Thank you for your answer ! I will try this. I'm sorry if I did short, I removed things on purpose to make the code more readable. My idea was to have a default sound for the superclass, wich I can change in the children classes, that's why I need to put a default one in a first time. Thanks again !
I'm trying to make the default constructor put the sound, something like this :
I have a NullPointerException, but still don't understand why...
You need a global variable of type Minim to use inside your class. This global variable should be initialised inside setup before trying to use Minim.
I have also altered the constructor so that you can have separate objects for each sound file.
Thanks again ! I still don't understand a thing. Why does this is allowed (I assume, I haven't tried yet) :
While this :
is not allowed and will give me errors (for sure) ?
edit : I tried both methods in a fresh new file. They are both working, so I made a mistake elsewhere in my code... I'll find it, thanks again !!! :)
That is NOT the problem that was an enhancement. The problem was that you missed this from setup
minim = new Minim(this);
Yes ok, sorry, I didn't saw your last answer. I found the problem :
I put the line :
Mynewobject myplayer = new Mynewobject();
in another .pde file (but in the same directory) By doing this, I got the same error ! I hope it will help people who have the same issue !
edit : just forget what I said, I'm doing only bad things. I think that
Mynewobject myplayer = new Mynewobject();
is not allowed, I have to split it in many parts