1

I need to implement Pause and Resume events of a MP3 player inside the same button click event. following is the code i have tried and its not working,Can any one give me the solution

private void button3_Click(object sender, EventArgs e)
{
    if (button3.Text == "Pause")
    {
        CommandString = "pause mp3file";
        mciSendString(CommandString, null, 0, 0);
        Status = true;
        button3.Text = "Resume";           
    }
    if (button3.Text == "Resume")
    {   
        CommandString = "resume mp3file";
        mciSendString(CommandString, null, 0, 0);
    }
}
4
  • 1
    What is not working? The if condition is not verified or the mciSendString function? If it's the mciSendString function you will have to describe what this function is, where it comes from, what it is supposed to do, etc... Commented Aug 23, 2010 at 8:04
  • 2
    Have you considered implementing a state machine? Commented Aug 23, 2010 at 8:05
  • "it's not working" is unfortunately not very helpful for us. Can you be more specific? Do you get errors? Does the pause work, but not the resume, or the other way around? Do the buttons fail, or the MP3? Exceptions, crashes? What happens when you place a breakpoint in the event handler, is it hit and does the execution follow the path you expect? Commented Aug 23, 2010 at 8:06
  • It appears that depending on the text on the button you decide which state your MP3 player is in. Whether or not this is a wise choice is a different question but it would be helpful to understand what mciSendString() does. I also agree with previous comments: "It's not working." isn't specific enough to provide good answers. Commented Aug 23, 2010 at 8:09

4 Answers 4

5

You are changing the button3.Text property within the first if statement. When the second if statement is tested it is true (both if statements are running with each button click when the Text property is "Pause")

Use if, else to run one code block or another.

Use if, else if statements if you want a test to be run on the second code block also.

You should also take account of the possibility that neither of these cases is true.

if (button3.Text == "Pause")
{
    CommandString = "pause mp3file";
    mciSendString(CommandString, null, 0, 0);
    Status = true;
    button3.Text = "Resume";
}
else if(button3.Text == "Resume")
{   
    CommandString = "resume mp3file";
    mciSendString(CommandString, null, 0, 0);
    button3.Text = "Pause";
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for good advice (but missed the change back to "Pause" though)
4

at first glance it won't work properly 'cause in 2 case

if (button3.Text == "Resume")
{   
    CommandString = "resume mp3file";
    mciSendString(CommandString, null, 0, 0);
}

you missed the line:

button3.Text = "Pause";

Actually it's not a good idea to check the button state by its text property. As a simple solution you need to have a boolean flag to check against it.

2 Comments

Ah... I missed that, but noticed the two if statements
@Lewray it's not you but the author :)
1

you have two if consecutive if statements. You need just one if/else statement.

change your code to:

    if (button3.Text == "Pause")
    {
        CommandString = "pause mp3file";
        mciSendString(CommandString, null, 0, 0);
        Status = true;
        button3.Text = "Resume";           
    }
    else if (button3.Text == "Resume")
    {   
        CommandString = "resume mp3file";
        mciSendString(CommandString, null, 0, 0);
    }

Comments

0

The problem is:

By the time you get to the second if statement, you have changed the Text of the button, hence both statements are being run...

Here is a quick test:

        if (button1.Text == "Pause")
        {
            label1.Text = label1.Text + " saw pause ";
            button1.Text = "Resume";


        }
        if (button1.Text == "Resume")
        {
            label1.Text = label1.Text + " saw resume ";
            button1.Text = "Pause";
        }

returns: label1 saw pause saw resume.

There are two ways to fix this:

You could insert a 'return;' statement within each if statement:

private void button3_Click(object sender, EventArgs e)
{

    if (button3.Text == "Pause")
    {
        CommandString = "pause mp3file";
        mciSendString(CommandString, null, 0, 0);
        Status = true;
        button3.Text = "Resume";
        return;
    }
    if (button3.Text == "Resume")
    {   
        CommandString = "resume mp3file";
        mciSendString(CommandString, null, 0, 0);
        button3.Text = "Pause";
        return;
    }
}

Or secondly, you could capture the value of the button text once:

private void button3_Click(object sender, EventArgs e)
{
    String value = button3.Text;
    if (value == "Pause")
    {
        CommandString = "pause mp3file";
        mciSendString(CommandString, null, 0, 0);
        Status = true;
        button3.Text = "Resume";

    }
    if (value == "Resume")
    {   
        CommandString = "resume mp3file";
        mciSendString(CommandString, null, 0, 0);
        buton3.Text = "Pause"; // As mentioned before, this is required too.
    }
}

Hope that helps.

Steve

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.