Click here to Skip to main content
16,021,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, So I am getting closer to the mystery of the unsolved issue (Pun)

So, for the past 3 days I have been trying to solve this issue & it is finally getting to me, it is making me frustrated & angry & I would like to throw the computer out of the window (Another bad pun)

What is going on is that I am trying to control my soundboard's volume with a trackBar.

it is not driven by Windows Media Player it is simply just some resources (Audio Files) It is a really boring project its really nothing special but I really cant seem to get the code to work.

(Will provide the .cs files if necessary) What I am trying to accomplish is that I want to make a trackBar that connects to the winForm app & lets the user control the volume of the WinForm itself.

Here is the code for the trackBar, or this is what I've gotten so far. I know its not much but I am really bad with trackBars.

Here is the code for the button & the sound being played when pressing it.

C#
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.Stream = Properties.Resources.cow;
player.Play();


& this is the code for the trackBar. I really cant seem to connect the two.
Any tips?
C#
private void trackBar1_Scroll(object sender, EventArgs e)
{
    MySoundPlayer.Volume = trackBar1.Value;
}
Posted
Updated 18-Nov-15 20:46pm
v2

1 solution

Hello again

Make this variable a member of your Form class
C#
private System.Media.SoundPlayer player;


Then in the constructor of the Form
C#
public MyForm()
{
    player = new System.Media.SoundPlayer();
}


Then in your button click code
C#
private void MyAwsomeButton_click(object sender, EventArgs e)
{
    player.Stream = Properties.Resources.cow;
    player.Play();
}


C#
private void trackBar1_Scroll(object sender, EventArgs e)
{
MySoundPlayer.Volume = trackBar1.Value;
    player.Volume = trackBar1.Value;
}

Now the two are connected.

Can you spot the difference?

[UPDATE]
So the SoundPlayer doesn't have a volume property.
Well I kind of thought you had checked that, but OK here is some reading for you.

A Synchronized Volume Control for your Application[^]

Controlling Sound Volume In C#[^]

Good luck with your quest.
 
Share this answer
 
v2
Comments
BladeLogan 19-Nov-15 3:20am    
Oh, Thank you so much, this has been bugging me for a very long time, Next time I'm making a SoundBoard I will not be using the SoundPlayer Class because it doesnt really have a Volume control!

-You learn something new everyday!
BladeLogan 19-Nov-15 3:39am    
By the way! I understand the concept & how it should work but it gives me the error:

CS1061 C# '' does not contain a definition for '' and no extension method '' accepting a first argument of type '' could be found (are you missing a using directive or an assembly reference?)

I'm assuming it is because the SoundPlayer Class doesnt have a volume control.
Please do correct me if I am wrong.
George Jonsson 19-Nov-15 5:28am    
Well, no need to assume. Just check the documentation for the SoundPlayer Class[^]
See my updated answer.
BladeLogan 19-Nov-15 8:03am    
Thank you so much, this is one of the big reasons why I love programming, besides from actually exploring such a wide subject & learning so much this is it, the great community & there is truly only a few people who actually provides the information & helps other people learn. Thank you sir!
George Jonsson 19-Nov-15 10:01am    
You are most welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900