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

2012/12/22

Adding Music to the background

Peach Peach

2012/12/22

#
how do you add music in the background while the scenario is running? i've tried the method Greenfoot.playSound("Spring.wav") because I want to play that song but a terminal window keeps popping up and says "could not open sound file". How can I fix this problem? thanks!
Gevater_Tod4711 Gevater_Tod4711

2012/12/22

#
It could be the case that you haven't got this soundfile in your sound folder. If you have got it you could try to convert it into mp3. This doesn't work always but most times.
Peach Peach

2012/12/22

#
thanks! i've got it but another problem has occurred. I want the music to start when the scenario starts, but now it starts when I open greenfoot. How can I make the music start only when I press 'run' and end when I press 'stop'?
vonmeth vonmeth

2012/12/22

#
Create methods in your world subclass called "public void started" and "public void stopped". When you click run, whatever is in "started" will run once, and when you click stop, whatever is in "stopped" will run once.
Peach Peach

2012/12/28

#
It doesn't work. But I'm not sure if i am doing it correctly. here's my code: import greenfoot.*; // imports Actor, World, Greenfoot, GreenfootImage import java.util.Random; import java.awt.Color; public class CrabWorld extends World { // public static final Color pathColor = new Color(227, 202, 148); /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ public CrabWorld() { super(560, 560, 1); Greenfoot.playSound("Spring.mp3"); populateWorld(); prepare(); getWidth(); getHeight(); } /** * Create the objects for the start of the game. */ public void populateWorld() { } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Counter counter = new Counter(); addObject(counter, 63, 28); Worm worm = new Worm(); addObject(worm, 452, 393); Crab crab = new Crab(counter); addObject(crab, 346, 415); Crab crab2 = new Crab(counter); addObject(crab2, 365, 186); Crab crab3 = new Crab(counter); addObject(crab3, 132, 198); crab2.setLocation(271, 186); removeObject(crab2); crab3.setLocation(500,400); addObject(new Crab(counter), 118, 254); addObject(new Crab(counter), 300, 300); addObject(new Lobster(), 390, 200); addObject(new Worm(), 200, 500); addObject(new Worm(), 30, 200); addObject(new Worm(), 60, 90); addObject(new Worm(), 80, 310); addObject(new Worm(), 150, 50); addObject(new Worm(), 290, 410); addObject(new Worm(), 220, 520); addObject(new Worm(), 380, 330); addObject(new Worm(), 410, 270); addObject(new Worm(), 130, 430); Worm worm12 = new Worm(); addObject(worm12, 394, 496); Worm worm13 = new Worm(); addObject(worm13, 319, 145); Worm worm14 = new Worm(); addObject(worm14, 513, 254); Worm worm15 = new Worm(); addObject(worm15, 179, 301); crab.setLocation(199, 415); } public void started (){ } public void stopped (){ } }
vonmeth vonmeth

2012/12/29

#
Remove Greenfoot.playSound("Spring.mp3").
private GreenfootSound bgMusic = new GreenfootSound("Spring.mp3");

    public void started()
    {
        bgMusic.playLoop();
    }

    public void stopped()
    {
        bgMusic.stop();
    }
Peach Peach

2012/12/29

#
thank you so much!! it finally works now.
You need to login to post a reply.