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

2016/5/7

Adding Resume Play to a Paused Scenario

1
2
Simvarghese Simvarghese

2016/5/7

#
This is the code I have so far. I need to figure out how to get the song to continue playing after I have stopped it. Thanks much!
public class Button extends Actor
{
    // Variables that are active for the entire Button class
    private static int songPlaying = 0; // The number (1, 2, ...) of the song playing, or 0 if no song is playing
    private static boolean isPaused = false;
    private static GreenfootSound gfs;         // The song to play
    
    // Instance variables that are active for each instance of a Button
    private String image;               // This button's image
    private String song;                // This button's song name, or blank if the button is not a song
    private int buttonType;             // 1, 2, 3, ... for a song button, -1 for play/pause, -2 for stop
    
    private boolean isMouseClicked;     // True if the mouse was clicked on this instance
    private GreenfootImage image1;
    private GreenfootImage image2;
    
    private String sound;
    private int setVolume;
    /**
     * Button constructor called when a new instance of a Button is created.
     */
    
    public Button(String imageName, String songName, int typeOfButton)
    {
        image = imageName;
        setImage(image);
        song = songName;
        
        buttonType = typeOfButton;
        image1 = new GreenfootImage("ButtonPlaySmall.png");
        image2 = new GreenfootImage("ButtonPauseSmall.png");
        sound=songName;
        gfs = new GreenfootSound(song);
    }
    
    /**
     * Act - do whatever the Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        isMouseClicked = Greenfoot.mouseClicked(this);
        checkSongPlaying();
    }   
    
 /**
     * checkSongPlaying - if a song is playing, see if we stop it. If a song is not playing, see if we start it
     */
    
    private void checkSongPlaying()
    {
        if (isMouseClicked)
        {
            getWorld().showText("Button type: " + buttonType + ", song name: '" + song + "'", 250, 320);
        }
         
        if (isMouseClicked && buttonType < 0)
        {
            if (songPlaying == 0) //no song is playing
            {
                getWorld().showText("Hey! There's nothing playing!", 250, 300);
            }
            else //song is playing
            {
                getWorld().showText("Pausing song " + songPlaying, 100, 380);
                gfs.pause();
                if (getImage() == image1) 
                    {
                     setImage(image2);
                    }
                else
                    {
                     setImage(image1);
                    }
                songPlaying= 0;
            }
        }
        
        if (isMouseClicked && songPlaying == 0 && buttonType > 0)
        {
            getWorld().showText("Starting song " + buttonType, 100, 380);
            gfs = new GreenfootSound(song);
            songPlaying = buttonType;
            gfs.play();
        }
        
        if (isMouseClicked && buttonType == -2)
        {
            if (songPlaying == 0)
            {
                getWorld().showText("Stopping song " + songPlaying, 100, 380);
                gfs.stop();
                songPlaying = 0;
            }
        }
   }
   
}
danpost danpost

2016/5/7

#
Line 89 seems amiss. Lines 91 tells me that you are stopping a song in that block of code; yet, if the value of 'songPlaying' is zero, then there is no song to stop.
Simvarghese Simvarghese

2016/5/7

#
I have a pause button and a play button. Lines 87-91 allow me to stop the song completely. I want to be able to resume the song if the pause button is pressed.
Simvarghese Simvarghese

2016/5/7

#
**I have a pause button and a stop button
danpost danpost

2016/5/7

#
Simvarghese wrote...
I have a pause button and a play button. Lines 87-91 allow me to stop the song completely. I want to be able to resume the song if the pause button is pressed.
I already understand that. I was just saying that if no song is selected and the stop button is pressed, you may get a run time error. As your code is now, the condition on line 89 will always be true when checked because of the earlier 'if' block starting at line 57. The value of 'songPlaying' either was already zero going into that block of code or it is set to zero within it. Line 75 seem to be the culprit. If pausing the song, you do not want to clear the song as being the selected one; you want to set the 'isPaused' field to true, instead.
Simvarghese Simvarghese

2016/5/7

#
Ok, I have changed the isPaused to be true and the problem still seems to be arising. When I play a song and press the pause button, the image of the pause button turns into a play button and the song is paused. When I press the play button again, it thinks that there is no song playing (songPlaying = 0), and doesn't play anything. How do I make it where when I press the play button again, it will continue the song where I paused it?
danpost danpost

2016/5/7

#
There seems to be an issue with the song fields. The 'gfs' field seems to be the song that is currently selected; yet, you are assigned a different song to it each time you create a button. If each button is to hold its own song, then you need a non-static field to hold a reference to them. Then, when the button is clicked, you can assign 'gfs' to that song. That would be something along these lines:
// class field for song playing (or paused)
static GreenfootSound gfs;

// instance field for song for this button
private GreenfootSound song;

// in constructor, instead of 'gfs =... ;'
song = ... ;

// when clicked
if (gfs != null) gfs.stop();
gfs = song;
if (!isPaused) gfs.play();
The 'songPlaying' field is not needed. The 'gfs' field is sufficient. Anyway, it is like a coded reference to the song and cannot be directly associated with that song. The 'gfs' field can be set to null when no song is selected. You can add this to keep the field up to snuff:
if (gfs != null && !isPaused && !gfs.isPlaying()) gfs = null;
This should catch the end of the song playing and clear the song selected.
danpost danpost

2016/5/7

#
Simvarghese wrote...
Ok, I have changed the isPaused to be true and the problem still seems to be arising. When I play a song and press the pause button, the image of the pause button turns into a play button and the song is paused. When I press the play button again, it thinks that there is no song playing (songPlaying = 0), and doesn't play anything. How do I make it where when I press the play button again, it will continue the song where I paused it?
I think line 75 is your answer here. Remove that line setting 'songPlaying' to zero.
Simvarghese Simvarghese

2016/5/7

#
sorry, I am a little lost as to where I should be inserting the code you mentioned above.
danpost danpost

2016/5/7

#
Well, you already have the 'static' field; you just need to add the instance field along with it. Then the last part goes in the 'if' block that has 'buttonType > 0' (line 79). The 'songPlaying == 0' can be removed from the conditions and lines 82 through 84 can be replaced by lines 11 through 13 of my post. If you want the song to start playing even if the last song was paused you can replace line 13 with this:
isPaused = false;
gfs.play();
Simvarghese Simvarghese

2016/5/7

#
So there is nothing I can change in lines 57-75 of my original code to just have the song continue to play where it left off? In the other discussion thread you said, "Now, set the value of 'gfs' in the constructor instead of in the 'checkSongPlaying' method so you will not be creating a new GreenfootSound object everytime you start the sound." What did you mean by that? Everything else in the code works perfectly as to how I want it.
danpost danpost

2016/5/7

#
Simvarghese wrote...
So there is nothing I can change in lines 57-75 of my original code to just have the song continue to play where it left off?
That is what you should have happening. Maybe you should show your revised code -- at least the 'checkSongPlaying' method.
Simvarghese Simvarghese

2016/5/7

#
 /**
     * checkSongPlaying - if a song is playing, see if we stop it. If a song is not playing, see if we start it
     */
    
    private void checkSongPlaying()
    {
        if (isMouseClicked)
        {
            getWorld().showText("Button type: " + buttonType + ", song name: '" + song + "'", 250, 320);
        }
         
        if (isMouseClicked && buttonType < 0)
        {
            if (songPlaying == 0) //no song is playing
            {
                getWorld().showText("Hey! There's nothing playing!", 250, 300);
            }
            else //song is playing
            {
                getWorld().showText("Pausing song " + songPlaying, 100, 380);
                gfs.pause();
                if (getImage() == image1) 
                    {
                     setImage(image2);
                    }
                else
                    {
                     setImage(image1);
                    }
                songPlaying=0;
                
        }
      }
        
      if (isMouseClicked && songPlaying == 0 && buttonType > 0)
        {
            getWorld().showText("Starting song " + buttonType, 100, 380);
            gfs = new GreenfootSound(song);
            songPlaying = buttonType;
            gfs.play();
            }
        
      if (isMouseClicked && buttonType == -2)
        {
            if (songPlaying == 0)
            {
                gfs.stop();
                getWorld().showText("Stopping song " + songPlaying, 100, 380);

            }
        }
I don't think I ever changed anything.
danpost danpost

2016/5/7

#
I mentioned this earlier, but I think it is on a different line, now. Change line 30 to:
ifPaused = true;
Line 75 seem to be the culprit. If pausing the song, you do not want to clear the song as being the selected one; you want to set the 'isPaused' field to true, instead.
Simvarghese Simvarghese

2016/5/7

#
import greenfoot.*;

/**
 * Write a description of class Button here.
 * 
 * @author  David Kramer 
 * @version 02
 */
public class Button extends Actor
{
    // Variables that are active for the entire Button class
    private static int songPlaying = 0; // The number (1, 2, ...) of the song playing, or 0 if no song is playing
    private static boolean isPaused= true;
    private static GreenfootSound gfs;         // The song to play 
    
    // Instance variables that are active for each instance of a Button
    private String image;               // This button's image
    private String song;                // This button's song name, or blank if the button is not a song
    private int buttonType;             // 1, 2, 3, ... for a song button, -1 for play/pause, -2 for stop
    
    private boolean isMouseClicked;     // True if the mouse was clicked on this instance
    private GreenfootImage image1;
    private GreenfootImage image2;
    
    private String sound;
    /**
     * Button constructor called when a new instance of a Button is created.
     */
    
    public Button(String imageName, String songName, int typeOfButton)
    {
        image = imageName;
        setImage(image);
        song = songName;
        
        buttonType = typeOfButton;
        image1 = new GreenfootImage("ButtonPlaySmall.png");
        image2 = new GreenfootImage("ButtonPauseSmall.png");
        
        sound=songName;
        gfs = new GreenfootSound(sound);
        
    }
    
    /**
     * Act - do whatever the Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        isMouseClicked = Greenfoot.mouseClicked(this);
        checkSongPlaying();
    }   
    
 /**
     * checkSongPlaying - if a song is playing, see if we stop it. If a song is not playing, see if we start it
     */
    
    private void checkSongPlaying()
    {
        if (isMouseClicked)
        {
            getWorld().showText("Button type: " + buttonType + ", song name: '" + song + "'", 250, 320);
        }
         
        if (isMouseClicked && buttonType < 0)
        {
            if (songPlaying == 0) //no song is playing
            {
                getWorld().showText("Hey! There's nothing playing!", 250, 300);
            }
            else //song is playing
            {
                getWorld().showText("Pausing song " + songPlaying, 100, 380);
                gfs.pause();
                if (getImage() == image1) 
                    {
                     setImage(image2);
                    }
                else
                    {
                     setImage(image1);
                    }
                ifPaused = true;
                
        }
      }
        
      if (isMouseClicked && songPlaying == 0 && buttonType > 0)
        {
            getWorld().showText("Starting song " + buttonType, 100, 380);
            gfs = new GreenfootSound(song);
            songPlaying = buttonType;
            gfs.play();
            }
        
      if (isMouseClicked && buttonType == -2)
        {
            if (songPlaying == 0)
            {
                gfs.stop();
                getWorld().showText("Stopping song " + songPlaying, 100, 380);

            }
        }
        
}
}
so this is what my full code looks like. is this correct?
There are more replies on the next page.
1
2