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

2015/12/7

How to pause music then resume

XxCelosiaxX XxCelosiaxX

2015/12/7

#
I'm trying to code it so that when a button is press a music will play when you press a pause button it will pause the music then turn into a play button then when you press it again resume the song from where it stop. How would I do something like that?
danpost danpost

2015/12/7

#
XxCelosiaxX wrote...
I'm trying to code it so that when a button is press a music will play when you press a pause button it will pause the music then turn into a play button then when you press it again resume the song from where it stop. How would I do something like that?
Show the classes that creates the buttons and checks for mouse clicks on the buttons. Cannot fix unseen code.
XxCelosiaxX XxCelosiaxX

2015/12/7

#
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import greenfoot.*;
 
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 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 Button instance
     
    /**
     * 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;
    }
     
    /**
     * 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);  // Keep the mouse clicking status handy
        checkSongPlaying();
        reduceTransparency();
    }   
     
    /**
     * 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)
            {
                getWorld().showText("Hey! There's nothing playing!", 250, 300);
            }
            else
            {
                getWorld().showText("Stopping song " + songPlaying, 100, 380);
                gfs.stop();
                songPlaying = 0;
                 
            }
        }
        if (isMouseClicked && songPlaying == 0 && buttonType > 0)
        {
            getWorld().showText("Starting song " + buttonType, 100, 380);
            gfs = new GreenfootSound(song);
            songPlaying = buttonType;
            gfs.play();
        }
    }
     
    /**
     * Reduce the transparency of the image of this instance is the song playing
     */
     
    public void reduceTransparency()
    {
        if (songPlaying == buttonType)
        {
            // Insert code here to reduce the transparency a bit, or if the lower transparency
            // limit has been reached, reset transparency to full transparency.
             
            // See the Chapter 10 'smoke' scenario in the Smoke class for details on reducing transparency
            getWorld().showText("Reducing transparency on song " + buttonType, 400, 380);
            return;
        }
    }
}
AngelusNeko13 AngelusNeko13

2015/12/7

#
hmmm.... lemme test somethin, this can be very useful for my game too
XxCelosiaxX XxCelosiaxX

2015/12/7

#
danpost wrote...
XxCelosiaxX wrote...
I'm trying to code it so that when a button is press a music will play when you press a pause button it will pause the music then turn into a play button then when you press it again resume the song from where it stop. How would I do something like that?
Show the classes that creates the buttons and checks for mouse clicks on the buttons. Cannot fix unseen code.
code above ^^^
danpost danpost

2015/12/7

#
Try 'pause' instead of 'stop'.
XxCelosiaxX XxCelosiaxX

2015/12/7

#
danpost wrote...
Try 'pause' instead of 'stop'.
well that still doesn't make the song continue from where it was pause
danpost danpost

2015/12/7

#
XxCelosiaxX wrote...
well that still doesn't make the song continue from where it was pause
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.
Simvarghese Simvarghese

2016/5/7

#
Can you explain your above comment in more detail please? I have the same code as XxCelosiaxX and I can't seem to resume the song after it has been paused.
danpost danpost

2016/5/7

#
Simvarghese wrote...
Can you explain your above comment in more detail please? I have the same code as XxCelosiaxX and I can't seem to resume the song after it has been paused.
Will reply on your discussion thread, here.
You need to login to post a reply.