This site requires JavaScript, please enable it in your browser!
Greenfoot back
Sneaky4296
Sneaky4296 wrote ...

2012/3/13

Sound Help

1
2
Sneaky4296 Sneaky4296

2012/3/13

#
Just for some kicks, I decided I would make myself a little music player. Problem is, I would like to stop the current song, if a new button is pressed. How would I go about doing this?
Duta Duta

2012/3/13

#
Is this using GreenfootSound or a Clip?
Sneaky4296 Sneaky4296

2012/3/13

#
My own mp3 files. I have them within the sounds folder, so they can be called using GreenfootSound
Duta Duta

2012/3/13

#
Ok so GreenfootSound: There's a pause method and there's also a stop method EDIT: Sorry forgot to actually answer the question: Make a button class (in which it has its image and everything) and then put an if(Greenfoot.mousePressed(this)) check in the act() method - in that if statements body use the pause()/stop() methods.
Sneaky4296 Sneaky4296

2012/3/13

#
How do I use the pause and stop methods? (Sorry. My programming teacher doesn't really do a whole lot in terms of the sound aspect.)
danpost danpost

2012/3/13

#
As long as you have a instance world GreenfootSound variable (in my case, called 'song') initialized to "", when a button is clicked, it can call a world method that recieves the String which contains the filename of the song, and the method can first check to see if 'song' is currently playing, and if so stop it, then set 'song' to the new GreenfootSound using the filename string, and start it. When a song is playing, 'song' will contain the GreenfootSound reference to the playing song, and can be paused or stopped with 'song.pause();' or 'song.stop();' Actually, you can get away without checking if a song is currently playing before stopping or pausing it, as stop() and pause() will not bomb if the song is already not playing.
Sneaky4296 Sneaky4296

2012/3/13

#
Thanks dan. I'm trying to make the GreenfootSound variable be really flexible, in terms of being able to be referenced from the other classes, so is it possible to make the sound a string variable, where the string variable is the sound to be played?
Sneaky4296 Sneaky4296

2012/3/13

#
Here's my code for the parent class: public class Buttons extends Actor { public GreenfootSound Sound = new GreenfootSound(^^^I want the variable 'Song' here^^^); String Song= "01 Lonely Boy.mp3"; /** * Act - do whatever the Buttons wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } }
danpost danpost

2012/3/13

#
The string would have to be used to create a GreenfootSound object before playing it, so, in a sense, yes. But, the flexibility will already be there when you add this method into the world class
public void setSong(String filename)
{
    song.stop();
    song = new GreenfootSound(filename);
    song.play();
}
You can call this from the Button class with
((Jukebox) getWorld()).setSong(filename);
where I used 'Jukebox' as the name of the sub-class of world and 'filename' as the sting containing the filename of the song.
danpost danpost

2012/3/13

#
A skeleton of your Buttons class should look like this
import greenfoot.*;

public class Buttons extends Actor
{
    String filename= "";

    public Buttons(String filenm)
    {
        filename = filenm;
        // code to construct the image of the button
    }

    public void act() 
    {
        if (Greenfoot.mouseClicked(this)) ((Jukebox) getWorld()).setSong(filename);
    }    
}
In the world, you would create a new button with
addObject(new Buttons("01 Lonely Boy.mp3"), x, y);
where x and y are actual values, or previously set integer variables. You then add more buttons using the different filenames. If the "01 " at the beginning of the filename is not actually part of the filename, then in line 15, in the Buttons class above, use 'setSong(filename.substring(3));' and 'song' in the world class will be set to 'Lonely Boy.mp3'.
Sneaky4296 Sneaky4296

2012/3/13

#
Can you explain this more in depth to me? Like an example couple of classes? I'm just learning and I don't really understand it... -Edit- I need to read what you just posted
Sneaky4296 Sneaky4296

2012/3/13

#
Classes so far...
import greenfoot.*;

public class Buttons extends Actor
{
    String filename= "";

    public Buttons(String filenm)
    {
        filename = filenm;
        // code to construct the image of the button
    }

    public void act() 
    {
        if (Greenfoot.mouseClicked(this)) ((Jukebox) getWorld()).setSong(filename);
    }    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Button here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Button extends Actor
{
    /**
     * Act - do whatever the Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        ((Jukebox) getWorld()).setSong(filename);
    }    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Jukebox here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Jukebox extends World
{
    String song= "01 Lonely Boy.mp3";
    /**
     * Constructor for objects of class Jukebox.
     * 
     */
    public Jukebox()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
    }
    public void setSong(String filename)
    {
        song.stop();
        song = new GreenfootSound(filename);
        song.play();
    }
}
Sneaky4296 Sneaky4296

2012/3/13

#
What do I do from here to make it functional?
danpost danpost

2012/3/13

#
The first thing is this: you have two button(s) classes, where you only need one. == Remove the one named "Button" (the one without the 's' at the end. == Change line 11 in the Jukebox world class to 'GreenfootSound song = null;' == Change line 23 in the Jubebox world (in setSong method) to 'if (song != null) song.stop();' == Insert immediately after 'super(600, 400, 1);' the 'addObject(new Buttons(...);' lines (one for each song) == Add the code to create the images of the buttons in the Buttons class constructor, maybe
GreenfootImage img = new GreenfootImage(150, 19);
img.drawRect(1, 1, 148, 17);
img.drawString(filename, 5, 15);
setImage(img);
for a very simple button. Place them in the world at the same x coordinate and 20 apart from each other vertically.
Sneaky4296 Sneaky4296

2012/3/13

#
It says it can't find method stop...
There are more replies on the next page.
1
2