• C# Sound Player
    6 replies, posted
Well, I have got a program and I need it to play a sound in an if statement, this is my code. [code]if (ReportRTB.Text != oldReports && !first && server == "gofish") notIcon.ShowBalloonTip(500, "New report", "A new report has been recieved on the Go Fish server.", ToolTipIcon.Info); else if (ReportRTB.Text != oldReports && !first && server == "underdone") notIcon.ShowBalloonTip(500, "New report", "A new report has been recieved on the Underdone server.", ToolTipIcon.Info);[/code] I added System.Media.SoundPlayer aSoundPlayer = new System.Media.SoundPlayer(@"\\Resources\\Sound\\New Report.wav"); in the if statement, it totally breaks it, gives me errors like. [code]Only assignment, call, increment, decrement, and new object expressions can be used as a statement[/code] [code]Invalid expression term 'else'[/code] [code]; expected[/code] That is the else if line.
@"\\Resources\\Sound\\New Report.wav" If you use @ you don't need double backslashes. [URL="http://www.c-sharpcorner.com/UploadFile/harishankar2005/verbatim_literals11262005010742AM/verbatim_literals.aspx"]Verbatim string literals.[/URL]
Ok thanks, anyone know why I am getting them errors?
Yeah, that's because the if-statement will only work for a single line of code without using brackets. Use brackets and you can also optimize the ifs and use a pretty switch by doing [cpp]if(ReportRTB.Text != oldReports && !first){ String servername; switch(server){ case "gofish": servername = "Go Fish"; break; case "underdone": servername = "Underdone"; break; default: servername = "invalid"; break; } notIcon.ShowBalloonTip(500, "New report", "A new report has been recieved on the " + servername + " server.", ToolTipIcon.Info); }[/cpp] Also, is there any reason why server cannot equal the server-name directly?
Thanks so much for that, well now, I need to put the sound code back in, I added it under the notIcon line like this. [code] notIcon.ShowBalloonTip(500, "New report", "A new report has been recieved on the " + servername + " server.", ToolTipIcon.Info); System.Media.SoundPlayer aSoundPlayer = new System.Media.SoundPlayer(@"\Report.wav");[/code] And it still wont work.
Well, you still have to call Play(Sync) on it. It's probably best if you store it as a private class member and call Load(Async) in the constructor, so the sound gets precached.
Thanks so much :D.
Sorry, you need to Log In to post a reply to this thread.