This is the code I have so far. I need to figure out how to get the song to continue playing after I have stopped it. Thanks much!
public class Button extends Actor
{
// Variables that are active for the entire Button class
private static int songPlaying = 0; // The number (1, 2, ...) of the song playing, or 0 if no song is playing
private static boolean isPaused = false;
private static GreenfootSound gfs; // The song to play
// Instance variables that are active for each instance of a Button
private String image; // This button's image
private String song; // This button's song name, or blank if the button is not a song
private int buttonType; // 1, 2, 3, ... for a song button, -1 for play/pause, -2 for stop
private boolean isMouseClicked; // True if the mouse was clicked on this instance
private GreenfootImage image1;
private GreenfootImage image2;
private String sound;
private int setVolume;
/**
* Button constructor called when a new instance of a Button is created.
*/
public Button(String imageName, String songName, int typeOfButton)
{
image = imageName;
setImage(image);
song = songName;
buttonType = typeOfButton;
image1 = new GreenfootImage("ButtonPlaySmall.png");
image2 = new GreenfootImage("ButtonPauseSmall.png");
sound=songName;
gfs = new GreenfootSound(song);
}
/**
* 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()
{
isMouseClicked = Greenfoot.mouseClicked(this);
checkSongPlaying();
}
/**
* checkSongPlaying - if a song is playing, see if we stop it. If a song is not playing, see if we start it
*/
private void checkSongPlaying()
{
if (isMouseClicked)
{
getWorld().showText("Button type: " + buttonType + ", song name: '" + song + "'", 250, 320);
}
if (isMouseClicked && buttonType < 0)
{
if (songPlaying == 0) //no song is playing
{
getWorld().showText("Hey! There's nothing playing!", 250, 300);
}
else //song is playing
{
getWorld().showText("Pausing song " + songPlaying, 100, 380);
gfs.pause();
if (getImage() == image1)
{
setImage(image2);
}
else
{
setImage(image1);
}
songPlaying= 0;
}
}
if (isMouseClicked && songPlaying == 0 && buttonType > 0)
{
getWorld().showText("Starting song " + buttonType, 100, 380);
gfs = new GreenfootSound(song);
songPlaying = buttonType;
gfs.play();
}
if (isMouseClicked && buttonType == -2)
{
if (songPlaying == 0)
{
getWorld().showText("Stopping song " + songPlaying, 100, 380);
gfs.stop();
songPlaying = 0;
}
}
}
}
