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

2014/12/9

Stopping a sound

christinem024 christinem024

2014/12/9

#
I'm trying to make it so that if a song is playing, and another song is selected, the first song stops playing. I created a refresh setting, but I'm confused about exactly what I've done wrong. PlayMusicButton Actor:
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;

public class PlayMusicButton extends Actor
{
    private String soundFiles;
    private String songNames;
    private Color colors;
    
    private static final int WIDTH = 220;
    private static final int HEIGHT = 40;

    private boolean selected;
    public boolean playing;
    
    private int song;
    
    public PlayMusicButton(String soundFiles, String songNames, Color colors)
    {
        this.soundFiles = soundFiles;
        this.songNames = songNames;
        this.colors = colors;
        selected = false;
        playing = false;
        
        makeImage();
    }
    
    public void act()
    {
        checkMouseClick();
    }
    
    public void refresh()
    {
        if(GreenfootSound.isPlaying()) 
        {
            GreenfootSound.stop(); 
        }
    }
    
    private void checkMouseClick()
    {
        if(!selected && Greenfoot.mouseClicked(this)) {
            PlayerWorld pw = (PlayerWorld)getWorld();
            pw.refresh();
            
            GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
            image.setColor(colors);
            image.fillRect(0, 0, WIDTH, HEIGHT);
            image.setColor(Color.BLACK);
            image.drawRect(0, 0, WIDTH-1, HEIGHT-1);

            image.setColor(Color.BLACK);
            Font font = image.getFont();

            image.setFont(new Font("Courier New", Font.BOLD, 22));
            image.drawString("" + songNames, 12, 22);
            setImage(image);
            selected = true;
            Greenfoot.playSound(soundFiles);
        }
        
        if(selected && !Greenfoot.mouseClicked(this)) {
            GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
            image.setColor(colors);
            image.fillRect(0, 0, WIDTH, HEIGHT);
            image.setColor(Color.BLACK);
            image.drawRect(0, 0, WIDTH-1, HEIGHT-1);

            image.setColor(Color.BLACK);
            Font font = image.getFont();

            image.setFont(new Font("Courier New", Font.PLAIN, 22));
            image.drawString("" + songNames, 12, 22);
            setImage(image);
            selected = true;
            Greenfoot.playSound(soundFiles);
            selected = false;
        }
    }
    
    public boolean isSelected()
    {
        return selected;
    }

    private void makeImage()
    {
        GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
        image.setColor(colors);
        image.fillRect(0, 0, WIDTH, HEIGHT);
        image.setColor(Color.BLACK);
        image.drawRect(0, 0, WIDTH-1, HEIGHT-1);

        image.setColor(Color.BLACK);
        Font font = image.getFont();

        image.setFont(new Font("Courier New", Font.PLAIN, 22));
        image.drawString("" + songNames, 12, 22);
        setImage(image);
    }
}
PlayerWorld world:
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;

public class PlayerWorld extends World
{ 
    private PlayMusicButton[] q = new PlayMusicButton[5];
    private static String[] soundFiles = {"Drive Me Home.mp3", "Get It On.mp3", "Hair.mp3", "Marry The Night.mp3", "Open Up Wide.mp3"};
    private static String[] songNames = {"Drive Me Home", "Get It On", "Hair", "Marry The Night", "Open Up Wide"};
    private static Color[] colors = {Color.yellow, Color.white, Color.lightGray, Color.pink, Color.cyan};
    
    public boolean isPlaying;
    
    public boolean isPlaying()
    {
        return playing;
    }
    
    public PlayerWorld()
    {
        super(600, 400, 1);

        addObject(new StopButton(), 480, 200);
        
        for(int i = 0; i < 5; i++)
        {
            addObject(new PlayMusicButton(soundFiles[i], songNames[i], colors[i]), 200, 50 + i * 75);
        }
        
        playing = false;
    }
    
    public void refresh()
    {
        q.refresh();
        
        for(int i = 0; i < 5; i++)
        {
            addObject(new PlayMusicButton(soundFiles[i], songNames[i], colors[i]), 200, 50 + i * 75);
        }
        
        if(isPlaying != null)
        {
            sound.stop();
            return;
        }
    }
}
christinem024 christinem024

2014/12/9

#
More specifically, I'm confused about this line from the PlayMusicButton. I'm confused because I don't know how to deal with this with a string of sounds:
public void refresh()
    {
        if(soundFiles.isPlaying()) 
        {
            soundFiles.stop();
            return;
        }
    }
christinem024 christinem024

2014/12/9

#
Also, how do I deal with this:
    public void refresh()
    {
        q.refresh();
        
        for(int i = 0; i < 5; i++)
        {
            addObject(new PlayMusicButton(soundFiles[i], songNames[i], colors[i]), 200, 50 + i * 75);
        }
        
        if(isPlaying != null)
        {
            sound.stop();
            return;
        }
    }
danpost danpost

2014/12/9

#
It can be a bit confusing -- especially because there are two different ways to have music play within greenfoot. One way is to use the Greenfoot class method 'playSound'; the other is to actually create a GreenfootSound object and use the methods provided in the GreenfootSound class to control it. Notice that by using the first way, you have no control over the sound -- you cannot even check if it is still playing or not because you do not retain a reference to the object playing.
christinem024 christinem024

2014/12/9

#
danpost wrote...
It can be a bit confusing -- especially because there are two different ways to have music play within greenfoot. One way is to use the Greenfoot class method 'playSound'; the other is to actually create a GreenfootSound object and use the methods provided in the GreenfootSound class to control it. Notice that by using the first way, you have no control over the sound -- you cannot even check if it is still playing or not because you do not retain a reference to the object playing.
@danpost is it possible to create a GreenfootSound object when you're referencing a string?
danpost danpost

2014/12/9

#
The GreenfootSound constructor needs a string representing the full filename of the sound file. It could be something like this:
GreenfootSound gfs = new GreenfootSound(soundFiles[i]+".mp3");
where you can then use 'gfs.play();', 'gfs.stop();', or any one of a number of object methods in the GreenfootSound class. The line, as given above, would be local to the method. You can move the declaration outside the method for persistence (so you can reference it at future times -- not only during the current act cycle). BTW, I like your new member profile fractal image.
christinem024 christinem024

2014/12/9

#
@danpost : I think I have the general idea but I get stuck at line 8 in the play music button. This is my new code:
danpost wrote...
The GreenfootSound constructor needs a string representing the full filename of the sound file. It could be something like this:
GreenfootSound gfs = new GreenfootSound(soundFiles[i]+".mp3");
where you can then use 'gfs.play();', 'gfs.stop();', or any one of a number of object methods in the GreenfootSound class. The line, as given above, would be local to the method. You can move the declaration outside the method for persistence (so you can reference it at future times -- not only during the current act cycle).
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;

public class PlayMusicButton extends Actor
{
    private String soundFiles;
    private String songNames;
    private Color colors;
    
    private static final int WIDTH = 220;
    private static final int HEIGHT = 40;

    private boolean selected;
    public boolean isPlaying;
    
    GreenfootSound gfs = new GreenfootSound(soundFiles + ".mp3");
    
    public PlayMusicButton(String soundFiles, String songNames, Color colors)
    {
        this.soundFiles = soundFiles;
        this.songNames = songNames;
        this.colors = colors;
        selected = false;
        
        makeImage();
    }
    
    public void act()
    {
        checkMouseClick();
    }
    
    private void checkMouseClick()
    {
        if(!selected && Greenfoot.mouseClicked(this)) {
            PlayerWorld pw = (PlayerWorld)getWorld();
            pw.isPlaying();
            
            GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
            image.setColor(colors);
            image.fillRect(0, 0, WIDTH, HEIGHT);
            image.setColor(Color.BLACK);
            image.drawRect(0, 0, WIDTH-1, HEIGHT-1);

            image.setColor(Color.BLACK);
            Font font = image.getFont();

            image.setFont(new Font("Courier New", Font.BOLD, 22));
            image.drawString("" + songNames, 12, 22);
            setImage(image);
            selected = true;
            gfs.play();
        }
        
        if(selected && !Greenfoot.mouseClicked(this)) {
            GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
            image.setColor(colors);
            image.fillRect(0, 0, WIDTH, HEIGHT);
            image.setColor(Color.BLACK);
            image.drawRect(0, 0, WIDTH-1, HEIGHT-1);

            image.setColor(Color.BLACK);
            Font font = image.getFont();

            image.setFont(new Font("Courier New", Font.PLAIN, 22));
            image.drawString("" + songNames, 12, 22);
            setImage(image);
            selected = true;
            
            selected = false;
        }
    }
    
    public boolean isSelected()
    {
        return selected;
    }

    public boolean isPlaying()
    {
        if(gfs.isPlaying = true)
        {
            gfs.stop();
            return;
        }
    }
    
    private void makeImage()
    {
        GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
        image.setColor(colors);
        image.fillRect(0, 0, WIDTH, HEIGHT);
        image.setColor(Color.BLACK);
        image.drawRect(0, 0, WIDTH-1, HEIGHT-1);

        image.setColor(Color.BLACK);
        Font font = image.getFont();

        image.setFont(new Font("Courier New", Font.PLAIN, 22));
        image.drawString("" + songNames, 12, 22);
        setImage(image);
    }
}
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;

public class PlayerWorld extends World
{ 
    private PlayMusicButton[] q = new PlayMusicButton[5];
    private static String[] soundFiles = {"Drive Me Home.mp3", "Get It On.mp3", "Hair.mp3", "Marry The Night.mp3", "Open Up Wide.mp3"};
    private static String[] songNames = {"Drive Me Home", "Get It On", "Hair", "Marry The Night", "Open Up Wide"};
    private static Color[] colors = {Color.yellow, Color.white, Color.lightGray, Color.pink, Color.cyan};

    GreenfootSound gfs = new GreenfootSound(soundFiles + ".mp3");
    
    public boolean isPlaying;
    
    public PlayerWorld()
    {
        super(600, 400, 1);

        addObject(new StopButton(), 480, 200);
        
        for(int i=0; i < 5 ; i++)
        {
            addObject(new PlayMusicButton(soundFiles[i], songNames[i], colors[i]), 200, 50 + i * 75);
        }
    }
}
danpost danpost

2014/12/9

#
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;

public class PlayMusicButton extends Actor
{
    private String soundFile;
    private String songName;
    private Color color;
    
    private static final int WIDTH = 220;
    private static final int HEIGHT = 40;

    private boolean selected;
    public boolean isPlaying;
    
    private GreenfootSound sound;
    
    public PlayMusicButton(String soundFile, String songName, Color color)
    {
        this.soundFile = soundFile;
        this.songName = songName;
        this.color = color;
        selected = false;
        sound = new GreenfootSound(soundFile+".mp3");
        makeImage();
    }
    // rest of method
}
This shows the adjustments to the beginning of your class. I presumes that your sound files are ".mp3" files -- if not, you need to make the necessary correction. This also does not presume that the rest of the class is fine and correct. I am quite sure that things will need to be tended to there as well. But, the above should get your fields (which I renamed to more properly fit what they represent) and their references set up properly. It seems a bit redundant to have two arrays with exactly the same data. You could probably eliminate either the 'soundFiles' or the 'songNames' array and use the same data for both creating the sound objects and displaying their names.
You need to login to post a reply.