How to add audio file to my form? - c#

I want to add an audio file to my form so that when any button event occurs, if anything is clicked then the audio file will play.
How can I do this?

code for adding audio?
private void playSoundFromResource(){
SoundPlayer sndPing = new SoundPlayer(SoundRes.GetType(), "Ping.wav");
sndPing.Play();
}

Well, a much more better way to write your code is to put the following code in form1.cs
private void Form1_Load(object sender, EventArgs e)
{
SoundPlayer Mcd = new SoundPlayer(#"C:\Users\Mcd\Desktop\abcd.wav");
Mcd.PlayLooping();
}

Related

c# VLC.Dotnet play and then pause issue

I hope someone will be able to help me with my issue. i want to take a screenshot of the video at a specific time index, however when i try to change the time all i get is a blank black screen. i added buttons which play and pause, it allows me to play and pause the video, if i do that and then change the time index, i get an image. im confused as to why it doesn’t work using code. i even preform btnplay.PeformClick(); to play the video and when i do btnpause.PerformClick() to pause the video it doesn’t.
it seems that i can only get an image of the video if i have to physically hit the play and then pause button on my form, im trying to achieve this using code
private void Form1_Load(object sender, EventArgs e)
{
////////////////////LC4 VLC Settings///////////////////////////////////////////////////////////////////////////////////////////
control = new VlcControl();
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
control.BeginInit();
control.VlcLibDirectory = libDirectory;
control.Dock = DockStyle.Fill;
control.EndInit();
panel1.Controls.Add(control);
main_form_LC4_data();
}
void main_form_LC4_data()
{
long vOut3 = 20;
playfile("path to file");
First_Frame(vOut3);
}
void playfile(string final)
{
control.SetMedia(new Uri(final).AbsoluteUri);
control.Time = 0;
control.Update();
}
void First_Frame(long vOut3)
{
control.Time = vOut3;
}
private void button9_Click(object sender, EventArgs e)
{
control.Play();
Console.WriteLine("PLAY");
}
private void button8_Click(object sender, EventArgs e)
{
control.Pause();
Console.WriteLine("PAUSE");
}
Above is my code in a nut shell
i have tried things like this
private void button10_Click(object sender, EventArgs e)
{
First_Frame(first_frame); // jump to index
}
and then calling up button10.PerformClick(); however it doesnt seem to work. once again if i physically hit the buttons on my form it works perfectly, however not in the way of coding it.
as an example :
play.PeformClick();
Pause.PeformClick();
time = vOut3;
I do hope this isnt to confusing im really stuck and am still hoping someone can help me
Thank you
A few things:
control.Update();
this does nothing.
You need to wait for the Playing event to be raised after setting the time, otherwise libvlc doesn't have the time to decode the frame and display it (setting the time is asynchronous)

How to change the image on a button

In my current project, I am trying to make a button change its image when a particular key is pressed. So far, my code looks something like
if (e.KeyCode == Keys.H)
{
button1.Image = bitmap.FromFile(C: filename\filename\filename\filename);
}
I don't know if I'm calling the file correctly, or using the right method or class. I'm still fairly new to this, so a simple explanation is probably best, thanks.
If you include the images to your resources you can do it like this:
public Form1()
{
InitializeComponent();
button1.MouseEnter += new EventHandler(button1_MouseEnter);
button1.MouseLeave += new EventHandler(button1_MouseLeave);
}
void button1_MouseLeave(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img1));
}
void button1_MouseEnter(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
}
I would not recommend hardcoding image paths.
For reference i use this link... maybe some other answer help you as well.
Reference: LINK

Turn a webcam off

Currently My Program is able to turn on the webcam, but right now I have no idea how to turn the webcam off by code.
Here's the code I use to capture some pictures:
private void webcamStart_Click(object sender, EventArgs e)
{
image = new Capture();
image.QueryFrame();
Application.Idle += new EventHandler(FrameGrabber);
}
PS: I using emguCV
Try This,not sure may be help you
Camera.Dispose()
I think this might work for you
image.Close()

How to play music in window form application by clicking button C#

I want to create a button, when the user click the button, the application can play music (played in embedded way or external way are both ok) I am just a rookie on c# and only know how to create a button Anyone know how to code it?
I found a way to run wav file
My.Computer.Audio.Play("wave.wav", AudioPlayMode.WaitToComplete)
However, I have added Microsoft.VisualBasic.dll" and
"Microsoft.DirectX.AudioVideoPlayback.dll into reference, the MY is still undefined.
Here is my button function
private void musicbutton_Click(object sender, EventArgs e)
{
{
SoundPlayer snd = null;
Stream str = Properties.Resources.mysong;
snd = new SoundPlayer(str);
snd.Play();
}
SoundPlayer snd = null;
private void musicbutton_Click(object sender, EventArgs e) {
Stream str = Properties.Resources.mySoundFile;
snd = new SoundPlayer(str);
snd.Play();
}
Inport the wav file into your Project Resources!

Defining a MouseEventHandler in C#

I'm trying to define MouseEventHandlers such that the application will exit whenever the mouse is clicked or moved or whenever a key is pressed. This is my first time using C#, but based on what I found online, I've written the code as follows:
MouseDown += new MouseEventHandler(mouseClickedResponse);
MouseMove += new MouseEventHandler(mouseMovedResponse);
KeyDown += new KeyEventHandler(keyResponse);
which connects to:
private void keyResponse(object sender, EventArgs e)
{
Application.Exit();
}
private void mouseClickedResponse(object sender, EventArgs e)
{
Application.Exit();
}
private void mouseMovedResponse(object sender, EventArgs e)
{
if (firstCall) //Keeps the application from exiting immediately
firstCall = false;
else Application.Exit();
}
The problem that I'm finding is that while the KeyEventHandler works perfectly, I can move and click the mouse as much as I want to no avail.
This is the sum total of the code that I've written to allow for user control; am I missing something?
On the surface, everything looks good with your code.
One possibility - The MouseEventHandler is defined in both the System.Windows.Input (MSDN) namespace as well as the System.Windows.Forms namespace (MSDN).
I believe the one you want is the one in the Forms namespace. Is it possible that you're using the one from the Input namespace instead?
I fixed my problem--my Form was filled with Panels, and by moving the code for mouse input over to the panels, everything worked instantly.
Change:
private void mouseClickedResponse(object sender, EventArgs e)
to:
private void mouseClickedResponse(object sender, MouseEventArgs e)
It should now work fine.

Categories