I'm currently building an mp3 with star/pause and volume up/down interaction. I have the basics completed, but the volume up and down are really buggy. For instance, one click on volume down nearly mutes the song, while a click on volume up as my first adjustment will lower initial volume but gradually increase with clicks. Any input for fixes or a better approach is welcome. Here is the code for my volume up
Here is volume down
Here is MyWorld
Here is VolumeBar, which I havent scripted yet implemented yet but is placed as a holder in MyWorld.
Any help on the volume level is appreciated.
Also, I'm wanting to build a volume bar by stacking a new segment when VolumeUp is clicked, and removing a segment when VolumeDown is clicked. Any suggestions on a sound way of doing so is also welcome. Thanks again.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class VolumeUp here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class VolumeUp extends Actor
{
private GreenfootSound song;
private int volume;
public VolumeUp(int masterVolume, GreenfootSound song ){
this.volume = masterVolume;
this.song = song;
}
/**
* Act - do whatever the VolumeUp wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
changeOnClick();
System.out.print(song.getVolume() + "\n");
}
public void changeOnClick(){
if (Greenfoot.mouseClicked(this)){
if(volume < 97){
volume += 3;
song.setVolume(volume);
}
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class VolumeDown here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class VolumeDown extends Actor
{
GreenfootSound song;
int volume;
public VolumeDown(int masterVolume, GreenfootSound song ){
this.song = song;
this.volume = masterVolume;
}
/**
* Act - do whatever the VolumeUp wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
changeOnClick();
}
public void changeOnClick(){
if (Greenfoot.mouseClicked(this)){
if(volume > 3){
volume -= 3;
song.setVolume(volume);
}
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld extends World
{
private GreenfootSound song = new GreenfootSound ("Canon.mp3");
private int masterVolume =25;
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
super(800, 600, 1);
build();
}
public GreenfootSound getSong(){
return song;
}
public int getMasterVolume(){
return masterVolume;
}
public void setMasterVolume(int newVolume){
this.masterVolume = newVolume;
}
public void build(){
GreenfootImage background = getBackground();
background.setColor(Color.BLACK);
background.fill();
StartButton play = new StartButton(song);
addObject(play,400,250);
VolumeUp volumeUp = new VolumeUp(masterVolume, song);
addObject(volumeUp, 650, 300);
VolumeDown volumeDown = new VolumeDown(masterVolume,song);
addObject(volumeDown, 150, 300);
VolumeBar volBar = new VolumeBar();
addObject(volBar, 150, 450);
}public class VolumeBar extends Actor
{
private int index;
public VolumeBar(){
}
/**
* Act - do whatever the VolumeBar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
}
}


