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

2014/11/2

sound problem

1
2
Alwin_Gerrits Alwin_Gerrits

2014/11/2

#
I'm trying to make some music play which obviously means it has to be started only once then play for a while. However, the command I'm using seems to react differently from how I found it should work. Here's my code:
1
2
3
4
5
6
7
8
9
10
GreenfootSound music = new GreenfootSound("music.wav");
 
if(music.isPlaying())
{System.out.println("music");} //if music is allready playing then do nothing
         
else if(!(music.isPlaying()))
 {
     music.play();
     System.out.println("no music");
}
The part that doesn't work seems to be music.isPlaying(). Which should return true if the music is allready started. However it seems to be working differently seen i constantly get the 'no music' output. Please your oppinion on this guys.
NikZ NikZ

2014/11/2

#
The code can be simplified to
1
2
3
4
5
if (!music.isPlaying()) 
 
     music.play(); 
     System.out.println("no music"); 
But I don't think it will solve the problem.
NikZ NikZ

2014/11/2

#
If no music output is only showing once, it should be fine: first the music plays then goes to the next line where it prints no music.
danpost danpost

2014/11/2

#
Do a search for the following string within the class: "GreenfootSound music" If you find it more than once, then you problem is probably that you have an instance field AND a local field with the same name for the same type object; however, the one that is playing is not the one you are checking.
NikZ NikZ

2014/11/2

#
If the code is in the act method, every time the act runs, music resets to a new GreenfootSound. Declaring music as private outside the act method is a solution to the problem.
danpost danpost

2014/11/2

#
Continuing my last post: You must have it declared as an instance field for your code to work (that is, as NikZ points out, outside the act method). Any value you wish to be held in a field from one act cycle to the next must be declared outside any methods/constructors.
Alwin_Gerrits Alwin_Gerrits

2014/11/2

#
K, so now I got this, but it's still printing 'no music' waaaay more then once.... And I think I got rid of everything you guys told me could cause problems... This is the everything within the actor's code that should be able to change behavour for my PlayMusic class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import greenfoot.*;
 
public class Sounds extends Actor
{
    GreenfootSound music = new GreenfootSound("music.wav");
 
    public void PlayMusic()
    {
        if(!(music.isPlaying()))
        {
            music.play();
            System.out.println("no music");
        }
    }
}
Just in case, the rest of the code looks simular to this, but I'm prety sure this kind of code can't cause the problem.
1
2
3
4
5
public void Jump()
    {
        GreenfootSound jump = new GreenfootSound("jump.wav");
        jump.play();
    }
Alwin_Gerrits Alwin_Gerrits

2014/11/2

#
I just tried making the rest of the code comment and indeed it didn't effect the problem at hand. So ignore the idea there's anything other then the first part of code in my previous post.
NikZ NikZ

2014/11/2

#
Is music playing properly?
Alwin_Gerrits Alwin_Gerrits

2014/11/2

#
Nope. If I let it run for more then a few seconds it starts soundling like it's beïng played 100 times at once. Which is probably the case...
Alwin_Gerrits Alwin_Gerrits

2014/11/2

#
Maybe it has something to do with the call? Seen it's an obvious non-static calling static issue I'm using
1
2
Sounds sounds = new Sounds();
sounds.PlayMusic();
Maybe that's part of the problem?
danpost danpost

2014/11/2

#
Hold on. Give me a second here.
danpost danpost

2014/11/2

#
Ok. Do not call 'sounds.PlayMusic();' after creating the Sounds object (your last two lines of code post). Do add the following into your Sounds class.
1
2
3
4
5
6
7
8
9
10
public void act()
{
    PlayMusic();
}
 
// you may need this method also
public void stopMusic()
{
    if (music.isPlaying()) music.stop();
}
If you do need to stop the music, add the 'stopMusic' method to your Sounds class and make sure that you have a reference to the Sounds object in the class you created it in (that is, add an instance field to hold it so you can refer to it when stopping it).
Alwin_Gerrits Alwin_Gerrits

2014/11/2

#
Sorry, that was my bad. I should have pointed out that the code I use to call 'PlayMusic' isn't in my Sounds class. It's a method within another class. That beïng said, your solution doesn't work seen I never add the object Sounds to my level. So the only way to make it play is by calling it from an object that's allways in my level. In this case that's player.
danpost danpost

2014/11/2

#
Then you do still need to keep a reference to the Sounds object created and call it from there. That is, DO NOT do this:
1
2
Sound sounds = new Sounds();
sounds.PlayMusic();
but DO this:
1
2
sounds = new Sounds();
sounds.PlayMusic();
Where 'sounds' is declared outside the method.
There are more replies on the next page.
1
2