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

2019/3/31

How to I make background music?

1
2
Poseidon420 Poseidon420

2019/3/31

#
I want background music to finish my game but I'm not sure how to do it.
Super_Hippo Super_Hippo

2019/3/31

#
You can create a GreenfootSound object. Check out the API for the class here: https://www.greenfoot.org/files/javadoc/greenfoot/GreenfootSound.html (Or double click on Actor or World in Greenfoot)
Poseidon420 Poseidon420

2019/3/31

#
How does one turn the song i want to play into a .wav file?
Super_Hippo Super_Hippo

2019/3/31

#
What file format is it? There are several online audio converters you can use.
Poseidon420 Poseidon420

2019/3/31

#
its a mp3 file
Poseidon420 Poseidon420

2019/3/31

#
Ive tried to put it in the main world but it says it cannot be found as a variable but i've put the file song in the sounds folder for the scenario.
Poseidon420 Poseidon420

2019/3/31

#
public void playLoop()
    {
        Greenfoot.playSound(Megalovania.mp3);
    }
theres the code
danpost danpost

2019/4/1

#
Poseidon420 wrote...
<< Code Omitted >> theres the code
The playSound method requires a String type argument. It also internally creates a new GreenfootSound object each time you call it. So, your looping will cause multiple copies of the sound to play one on top of another. You should create one instance of it yourself so you can control that one, and only one, instance of it:
private GreenfootSound music = new GreenfootSound("Megalovania.mp3");

public void playLoop()
{
    music.playLoop();
}
Super_Hippo Super_Hippo

2019/4/1

#
MP3 is fine. No need to convert it to WAV. The parameter has to be a String:
Greenfoot.playSound("Megalovania.mp3");
The method name doesn't represent what it does, though. It is only playing it once. If you ever want to pause/stop the sound, you should create a GreenfootSound object and keep a reference to it. playSound should only be used for sounds, not for music. The following code will loop the music while the scenario is running and pause the music when while the scenario is paused.
//in active world
private GreenfootSound bgMusic = new GreenfootSound("Megalovania.mp3");

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

public void stopped()
{
    bgMusic.pause();
}
Poseidon420 Poseidon420

2019/4/2

#
its still not playing and i've tried both codes.
danpost danpost

2019/4/2

#
Poseidon420 wrote...
its still not playing and i've tried both codes.
Maybe my BGMusic Class Demo scenario can give you some ideas. Or, maybe you can make direct use of the class itself.
Poseidon420 Poseidon420

2019/4/2

#
It worked now i just misspelt a bit of code. Thanks guys
TheGoldenProof TheGoldenProof

2019/4/3

#
Related to this, I have some background music, but it doesn't loop when I use playLoop. Any suggestions or should i start a new thread with my code?
Super_Hippo Super_Hippo

2019/4/3

#
Doesn't matter, but you need to show your code.
TheGoldenProof TheGoldenProof

2019/4/3

#
Super_Hippo wrote...
Doesn't matter, but you need to show your code.
Since its implementation is rather complex, I think I'll keep using my roundabout isPlaying() check solution.
There are more replies on the next page.
1
2