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?
public void setSong(String filename)
{
song.stop();
song = new GreenfootSound(filename);
song.play();
}((Jukebox) getWorld()).setSong(filename);
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);
}
}addObject(new Buttons("01 Lonely Boy.mp3"), x, y);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();
}
}
GreenfootImage img = new GreenfootImage(150, 19); img.drawRect(1, 1, 148, 17); img.drawString(filename, 5, 15); setImage(img);