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?


1 2 3 4 5 6 | public void setSong(String filename) { song.stop(); song = new GreenfootSound(filename); song.play(); } |
1 | ((Jukebox) getWorld()).setSong(filename); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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); } } |
1 | addObject( new Buttons( "01 Lonely Boy.mp3" ), x, y); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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(); } } |
1 2 3 4 | GreenfootImage img = new GreenfootImage( 150 , 19 ); img.drawRect( 1 , 1 , 148 , 17 ); img.drawString(filename, 5 , 15 ); setImage(img); |