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

2016/5/8

How do I pause and stop a song playing?

chrisbsports chrisbsports

2016/5/8

#
Currently I have 5 buttons that play songs. The pause and stop buttons are not executing the pause and stop commands. How do I get them to execute the pause and stop commands?
public class Button extends Actor
{
    private static int songPlaying = 0; 
    private static boolean isPaused = false;
    
    private String image;               
    private String song;                
    private int buttonType;             
    private GreenfootSound gfs;         
    private boolean isMouseClicked;     
    
    public Button(String imageName, String songName, int typeOfButton)
    {
        image = imageName;
        setImage(image);
        song = songName;
        gfs = new GreenfootSound(song);
        buttonType = typeOfButton;
        
    }
    
    public void act() 
    {
        isMouseClicked = Greenfoot.mouseClicked(this);
        checkSongPlaying();
       
    }    

        private void checkSongPlaying()
    {
        if (isMouseClicked)
        {
            getWorld().showText("Button type: " + buttonType + ", song name: '" + song + "'", 250, 320);
            gfs.play();
            
        }
         
        if (isMouseClicked && buttonType < 0)
        {
            if (songPlaying == 0)
            {
                getWorld().showText("Song is Stopped!", 250, 380);
                gfs.stop();
            }
            else
            {
                getWorld().showText("Song is Paused " + songPlaying, 250, 380);
                songPlaying = 0;
                isPaused=false;
  
            }
            

        }
        if (isMouseClicked && songPlaying == 0 && buttonType > 0)
        {
            getWorld().showText("Starting song " + buttonType + " named " + song, 250, 380);
            songPlaying = buttonType;
        }
    }
    

}
Super_Hippo Super_Hippo

2016/5/8

#
public void started()
{
    gfs.play();
}

public void stopped()
{
    gfs.pause();
}

danpost danpost

2016/5/8

#
Each button created in your project is getting a 'gfs' field of its own -- even the 'play', 'pause' and 'stop' buttons. So, calling 'gfs.stop()' or 'gfs.pause()' (see lines 43 and ?? wherever it disappeared to ??) will not call the stop and pause methods on the song that is currently playing. A problem with acquiring that particular song is found when you realize that only the number of the song is available in the 'songPlaying' class field. Maybe that field should be replaced with something a little more useful. If you save the GreenfootSound object, you will not keep the song number. What is left? -- how about the Button itself. By holding the song button in a class field, its 'buttonType' and its 'gfs' will be available. This would be what is needed since you apparently need access to both those values. To make them accessible, however, either lines 8 and 9 need to be made 'public' instead of 'private' or public getter method will need to be added to the class to allow access to their values. So, you will be removing the 'songPlaying' field altogether and replace it with a 'private static Button songButton' field. Then, you will need to make the appropriate changes within the code (like changing the appropriate occurences of 'buttonType' to 'songButton.buttonType' and change the appropriate 'gfs' occurrences to 'songButton.gfs', etc.). These occurrences will be located in the code of the control buttons when referring to the current selection.
chrisbsports chrisbsports

2016/5/8

#
Ok thanks for the feedback!
danpost danpost

2016/5/8

#
You cannot use the 'gfs' field for both the song assigned to a button AND the song playing. You need a separate field for each.
// class field for playing song
public static GreenfootSound gfs;

// instance field for button song
private GreenfootSound sound;

// change line 17 in Button class to the following
sound = new GreenfootSound(song);

// insert this at line 35 in Button class
gfs = sound;
That should get you closer.
chrisbsports chrisbsports

2016/5/9

#
I made some major changes to make it more simplistic. A problem I have now is my first song from my string won't play. Another problem is making the Stop button execute the stop method when a song is playing, but ignoring the method when no song is playing.
public class Button extends Actor
{
    private static int songPlaying =0; 
    private static boolean isPaused = false;
    
    private String image;               
    private String song;                
    private int buttonType;             
    private static GreenfootSound gfs;         
    private boolean isMouseClicked; 
    
    
    public Button(String imageName, String songName, int typeOfButton)
    {
        image = imageName;
        setImage(image);
        song = songName;
        //gfs = new GreenfootSound(song);
        buttonType = typeOfButton;
        
    }
    
    public void act() 
    {
        isMouseClicked = Greenfoot.mouseClicked(this);
        checkSongPlaying();
       
    }    

    private void checkSongPlaying()
    {
        if (!Greenfoot.mouseClicked(this))
        {
            songPlaying = buttonType;
            return;
        }
        // If we are here, the mouse button was clicked
        if (buttonType == -1)   // i.e., was the pause button clicked?
        {
            if (songPlaying == 0)
            {             
                gfs.play();
                return;                 
            }            
            // Here, we pressed the pause button and the song
            // is playing            
            if (isPaused) 
            {
                gfs.play();
                isPaused = false;
                getWorld().showText("Song Playing", 250, 380);
            }
            else
            {
                // We're a playing a song that is NOT paused
                // and we have to pause it
               //GreenfootSound.pause();
               gfs.pause();
               isPaused = !false;
               songPlaying = buttonType;
               getWorld().showText("Song Paused", 250, 380);
            }
            return;
        }
        
        if (buttonType == -2) // i.e., the stop button was pressed
        {
            gfs.stop();
            getWorld().showText("Song Stopped", 250, 380);
            return;
        }
        
        // Here, the mouse was clicked, and it wasn't the pause
        // or stop button. So it MUST be a song button
        // We know that buttonType > 0 (a song button)
        
        if (songPlaying > 0)
        {
            getWorld().showText("Starting song " + buttonType, 100, 380);
            gfs = new GreenfootSound(song);
            gfs.play();
            songPlaying = buttonType;
            isPaused = false;
        }

    }


}
danpost danpost

2016/5/9

#
First thing I noticed is that lines 32 through 36 looks funny. It is setting 'songPlaying' to the number of the button when the mouse is NOT clicked on this button -- seems a bit off (like not something you would want to do). You should probably just remove those lines. To arrange things a bit better, change the act method to:
public void act()
{
    if (Greenfoot.mouseClicked(this)) checkSongPlaying();
}
This should compensate for the removal of those lines. Another things is I do not see how the value of 'songPlaying' is relevant to the control buttons (start, pause and stop). It is only the reference of 'gfs' that is important to them. What is important are these things: * is the 'gfs' field currently referencing a song or does it contain a 'null' value * is the 'isPaused' field set to 'true' or 'false' You may or may not need some other information, but these two will certainly be used for one or more of the control button click actions.
You need to login to post a reply.