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

2016/11/1

MP3 player with Vol up and Vol down buttons

ED1990 ED1990

2016/11/1

#
I am working on a basic MP3 player that uses a volume down & volume up button. I have been able to get the volume to increase but it only increases more when I use the "-" instead of "+". Any assistance would be appreciated. below is Myworld which extends World
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(300, 75, 1);
        setup();
    }
    private void setup()
    {
        musik = new GreenfootSound("California.wav");
           
        Start play = new Start(musik);
        addObject(play,35,37);
                    
        VolUp volumeUp = new VolUp(musik);
        addObject(volumeUp,120,37);
   
        VolDown volumeDown = new VolDown(musik);
        addObject(volumeDown,200,37);
           
        Volume volume = new Volume(musik);
        addObject(volume, 260, 45);
        Message msg = new Message(text);
        addObject(msg, 260, 15);
    }
The following is my VolDown code:
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
28
29
30
31
32
33
34
35
36
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class VolDown here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class VolDown extends Actor
{
 
    private GreenfootSound music;
    /**
     * Act - do whatever the VolumeUp wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public VolDown (GreenfootSound musik)
    {
        this.music = musik;
    }
   
    public void act()
    {
        isClicked();
    }  
   
    public void isClicked()
    {
        if(Greenfoot.mouseClicked(this))
        {
            //System.out.print(music.getVolume() + " initVolDn \n"); //Testing only
            music.setVolume(music.getVolume()-10);
            //System.out.print(music.getVolume() + " VolDn \n"); //Testing only
        }
    }
}
danpost danpost

2016/11/2

#
ED1990 wrote...
I have been able to get the volume to increase but it only increases more when I use the "-" instead of "+".
The code given appears to be okay. What might you have in the Volume and VolUp classes?
ED1990 ED1990

2016/11/2

#
The VolUp is identical to VolDn with the exception of +10 vs -10, and the Volume class is just there to give a visual display of volume level.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Volume extends Actor
{
    public int volume;
    private GreenfootSound music;
     
     
    public Volume(GreenfootSound musik)
    {
        this.music = musik;
    }
     
    public void act()
    {
        volume = music.getVolume();
        setImage(new GreenfootImage("" + volume, 30, Color.WHITE, Color.BLUE));
         
                 
    }
}
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
public class VolUp extends Actor
{
    private GreenfootSound music;
     
    public VolUp (GreenfootSound musik)
    {
        this.music = musik;
    }
   
    public void act()
    {
        isClicked();
    }  
   
    public void isClicked()
    {
        if(Greenfoot.mouseClicked(this))
        {
             
            //System.out.print(music.getVolume() + " initVolUp \n");
            music.setVolume(music.getVolume()+ 10);
            //System.out.print(music.getVolume() + " VolUp \n");
        }
    }
}
I spent some more time with this last evening and was able to get the sound to decrease if I use
1
music.setVolume((music.getVolume()-10) / 2);
but it only decreases so far (Example... I can increase to 97-100 depending, but can only get it down to around 67)
danpost danpost

2016/11/2

#
I kind of figured that the Volume class was for displaying the current volume -- just wanted to be sure of that and that there was nothing there that might influence the current volume. From your last post, it appears that there may be a bug in the implementation greenfoot has for sound volume with your version.
You need to login to post a reply.