-1

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();
}
user3079834
  • 2,009
  • 2
  • 31
  • 63
Gif
  • 23
  • 3
  • so why can't you write your code using `1 Button` and then on the first button Click change the text to `'Paused` after clicking a second time check if the text of the button `.Contains("Paused")` then set back to "Start" and do some additional logic.. I don't see what the issue is here..? or learn to use booleans correctly.. – MethodMan May 19 '17 at 14:36
  • Basically you want a way to check if the song is playing or not. Ideally you will have a method that can check the state of the song. Like `GetIsPlaying()` then you change your action based on the result of that function. Easy way is to track the state with a bool property `IsPlaying` and query that, each time you press the button you flip the property `IsPlaying = !IsPlaying;` and update the button text too – musefan May 19 '17 at 14:37

2 Answers2

5

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;
}
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
0

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!

Dead Community
  • 157
  • 1
  • 13