How to combine two buttons

834 views Asked by At

There is button_play and button_pause. I want to combine them into one button. The first time the song is pressed, the song starts playing. The second press - pause. At the third press, the playback continues. I can not do it.

Please tell me, how I can combine them.

private void button_play_Click(object sender, EventArgs e)
{
    if ((list_catalog.Items.Count != 0) && (list_catalog.SelectedIndex != -1))
    {
        string current = Vars.Files[list_catalog.SelectedIndex];
        Vars.CurrentTrackNumber = list_catalog.SelectedIndex;
        BassLike.Play(current, BassLike.Volume);
        label_time1.Text = TimeSpan.FromSeconds(BassLike.GetPosOfStream(BassLike.Stream)).ToString();
        label_time2.Text = TimeSpan.FromSeconds(BassLike.GetTimeOfStream(BassLike.Stream)).ToString();
        xrewind.Maximum = BassLike.GetTimeOfStream(BassLike.Stream);
        xrewind.Value = BassLike.GetPosOfStream(BassLike.Stream);
        timer1.Enabled = true;
    }
}

private void button_pause_Click(object sender, EventArgs e)
{
    BassLike.Pause();
}
2

There are 2 answers

4
Jeroen van Langen On BEST ANSWER

Something like:

private bool _isPlaying;

private void button_Click(object sender, EventArgs e)
{
    if(!_isPlaying)
    {
        mediaThing.Play();
        button1.Text = "Pause";
    }
    else
    {
        mediaThing.Pause();
        button1.Text = "Play";
    }

    _isPlaying = !_isPlaying;
}
0
Dead Community On

The easiest way is to create a new Button object, and add the proper method for displaying the correct image. Something like this:

public class ButtonStateHandler:MonoBehaviour {
public boolean isClicked;
public Button myBtn;
public Sprite Play;
public Sprite Pause;

public void Click(){
    changeState();
}
private void changeState(){
    isClicked = !isClicked;
    if(isClicked)       myBtn.image.sprite = Play;
    else myBtn.image.sprite = Pause;
    }
}

Hope this helps!