Windows media player play states in C# - c#

I´m beginner in C# coding. Especially, when it comes to use Wmp in C#. I work in visual studio 2017, on a project called "station information system" for a small, private railway in our country. The whole functionality of program I´ve done correctly, it works, so now I need to come up with an idea, how to make it playing. My program exports exact names of mp3 files into a hidden listbox. To generate patches I use this code:
axWindowsMediaPlayer1.URL = Path.Combine(Environment.CurrentDirectory, #"ZVUK\", listBox2.SelectedItem.ToString());
It works, so I came to the other functionality I need from Wmp. When Wmp plays the full song, I need it to change the index in listbox and go to second song, then third and so on. I used this condition:
if (axWindowsMediaPlayer1.playState() = WMPLib.WMPPlayState.wmppsMediaEnded)
The part "playState()" is underlined and it shows me an error: "Non-invocable member "axWindowsMediaPlayer1.playState" cannot be used like a method." I tried deleting brackets but it just underlined the whole part "axWindowsMediaPlayer1.playState". Can someone help me?
Thank you very much.

You can use the following code to play the file one by one.
private void button1_Click(object sender, EventArgs e)
{
SoundPlayer player = new SoundPlayer(#"2.wav");
player.PlaySync();
player = new SoundPlayer(#"\1.wav");
player.PlaySync();
}

Related

Kinect Hovering event handler

I am creating a WPF application using Kinect SDK 1.8
I want to press a button with hand.
private void playTeeth1Sound(object sender,RoutedEventArgs e)
{
System.Media.SoundPlayer teeth1_Sound = new System.Media.SoundPlayer(#"../../soundForKinect/1.wav");
teeth1_Sound.Play();
}
I don't see any Kinect specific code in your question, and I'm not sure what the questions is, but one thing to consider in the code snippet in your question is:
Every time this method is called (presumably when the button is pressed), it has to:
instantiate a new System.Media.SoundPlayer
load the sound file into teeth1_Sound
play the sound file with the Play() method
You might find there is a delay each time as the code re-instantiates teeth1_Sound and reloads the sound file? It might be easier to load all the sound files when the app is starting up, have them ready to Play() as soon as you need them?

Using the VLC ActiveX plugin to watch youtube videos C#

Recently I have started messing around with the VLC ActiveX plugin trying to play youtube videos in a WindowsFormApplication and I have run into some issues that I couldn't find any mention of or solution to. For simplicity I made a new project to demonstrate my problems:
private void button1_Click(object sender, EventArgs e)
{
Player.playlist.play();
}
private void button2_Click(object sender, EventArgs e)
{
url = textBox1.Text;
Player.playlist.add(url);
listBox1.Items.Add(url);
}
2 buttons, play and add to playlist.
Problem 1:
Audio cuts out a few seconds before the end of the video.
Problem 2:
I have been unable to get the control to play the next video in the playlist. It just reaches the end of the first video. If I click the play button again, it plays the first video. One thing I thought might be causing it was the AutoPlay property but it's set to true.
Problem 3:
The Player.playlist.next function, like most thing doesn't have a description and does not let me go to the next video in the playlist.
Problem 4:
The AutoLoop property does not work, assuming that it's supposed to get the control to loop a video.
Problems number 2 and 3 make me think that the songs aren't being added to the playlist correctly, but again I was unable to find any way to confirm or solve that issue.
Using VisualStudio 2015, VLC plugin version, as stated in the control properties, 3.0.1 Vetinari.
(edit) after testing I know that the videos are added to the playlist, but still it won't autoplay the next in playlist at the end.

Volume Control ValueChanged in Form_Load event & trackBar .NET Visual Studio C#

First off I would like to say that I am kinda new to visual studio C# (2 months in) & I've studied other languages but I know the fundimentals & the project is somewhat done, its just this one feature I cant seem to get the hang of.
So I am trying to connect a trackBar to my WinForm application which is a SoundBoard.
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.
I know I need to set the min and max values according to my needs at the beginning, like in the Form_Load event.
E.g. the volume control uses percentage 0 - 100%
Then I need to set min=0, max=100.
the thing is, I have no idea how to do it, I have never seen any code that makes any sence in this scenario.
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.
(Sorry for my bad english, not my native tounge.)
private void trackBar1_Scroll(object sender, EventArgs e)
{
trackBar1.Minimum = 0;
trackBar1.Maximum = 100;
}
This is the code for the sounds being played with each button that is named differently.
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.Stream = Properties.Resources.cow;
player.Play();
There is no sense in modifying the Minimum and Maximum values within an Scroll event. Just set these values to 0 and 100 in designer (Properties window).
Then, you will be able to use this event and control the volume through it:
private void trackBar1_Scroll(object sender, EventArgs e)
{
yourSoundPlayer.Volume = trackBar1.Value;
}
As you haven't told what you use for playing a sound, I have assumed the Volume property. However, it may be another in your case.

windows media player c#

I am very new at c# and visual studio and now i am learning all the tools.
At first i tried adding background sound to my form. I added a wav file trhough resources and i changed it's build action to embedded resources so that i can "play" the .exe file without problems at any other pc.Then i wrote this and it worked.
namespace audio
{
public partial class Form1 : Form
{
SoundPlayer sp = new SoundPlayer(Properties.Resources.back_sound);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
sp.Play();
}
}
}
Now i want to play a video file with windows media player.I added it through COM components and i added it to my form.I can make it play any video just by adding the video file at the debug folder and write this.
private void button1_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = "video.AVI";
}
But as before i want to add it to my resources so that i can execute the .exe file to aby other pc and by this way, i cant
I tried with ( following the same pattern with the sound file)
AxWindowsMediaPlayer player = new AxWindowsMediaPlayer(Properties.Resources.video);
( and inside the button player.play();)
and
WindowsMediaPlayer player = new WindowsMediaPlayer(Properties.Resources.video);
( and inside the button player.play();)
but no luck.
Can anyone help me, please?
Thanks in advance
UPDATE:
I tried a lot of things and nothing worked, so i thought that it would better to stream the videos directly from youtube. I went to COM components and i added to my form a SHOCKWAVE FLASH OBJECT. I took the url i wanted, i transformed it ( from www.youtube.com/watch?v=xxxxxxxxxxxx to www.youtube.com/v/xxxxxxxxxxxx) i added it to the movie section of the flash object. It is working but only on my computer. If i execute the .exe in any other pc it doesnt work. It says that System.IO.FileNotFoundException.
What can i do to make it work?

How to play a sound in C#, .NET

I have a Windows application written in C#/.NET.
How can I play a specific sound when a button is clicked?
You could use:
System.Media.SoundPlayer player = new System.Media.SoundPlayer(#"c:\mywavfile.wav");
player.Play();
You can use SystemSound, for example, System.Media.SystemSounds.Asterisk.Play();.
For Windows Forms one way is to use the SoundPlayer
private void Button_Click(object sender, EventArgs e)
{
using (var soundPlayer = new SoundPlayer(#"c:\Windows\Media\chimes.wav")) {
soundPlayer.Play(); // can also use soundPlayer.PlaySync()
}
}
MSDN page
This will also work with WPF, but you have other options like using MediaPlayer MSDN page
Additional Information.
This is a bit high-level answer for applications which want to seamlessly fit into the Windows environment. Technical details of playing particular sound were provided in other answers. Besides that, always note these two points:
Use five standard system sounds in typical scenarios, i.e.
Asterisk - play when you want to highlight current event
Question - play with questions (system message box window plays this one)
Exclamation - play with excalamation icon (system message box window plays this one)
Beep (default system sound)
Critical stop ("Hand") - play with error (system message box window plays this one)
 
Methods of class System.Media.SystemSounds will play them for you.
 
Implement any other sounds as customizable by your users in Sound control panel
This way users can easily change or remove sounds from your application and you do not need to write any user interface for this – it is already there
Each user profile can override these sounds in own way
How-to:
Create sound profile of your application in the Windows Registry (Hint: no need of programming, just add the keys into installer of your application.)
In your application, read sound file path or DLL resource from your registry keys and play it. (How to play sounds you can see in other answers.)
Code bellow allows to play mp3-files and in-memory wave-files too
player.FileName = "123.mp3";
player.Play();
from http://alvas.net/alvas.audio,samples.aspx#sample6 or
Player pl = new Player();
byte[] arr = File.ReadAllBytes(#"in.wav");
pl.Play(arr);
from http://alvas.net/alvas.audio,samples.aspx#sample7
To play an Audio file in the Windows form using C# let's check simple example as follows :
1.Go Visual Studio(VS-2008/2010/2012) --> File Menu --> click New Project.
2.In the New Project --> click Windows Forms Application --> Give Name and then click OK.
A new "Windows Forms" project will opens.
3.Drag-and-Drop a Button control from the Toolbox to the Windows Form.
4.Double-click the button to create automatically the default Click event handler, and add the following code.
This code displays the File Open dialog box and passes the results to a method named "playSound" that you will create in the next step.
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Audio Files (.wav)|*.wav";
if(dialog.ShowDialog() == DialogResult.OK)
{
string path = dialog.FileName;
playSound(path);
}
5.Add the following method code under the button1_Click event hander.
private void playSound(string path)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = path;
player.Load();
player.Play();
}
6.Now let's run the application just by Pressing the F5 to run the code.
7.Click the button and select an audio file. After the file loads, the sound will play.
I hope this is useful example to beginners...
I think you must firstly add a .wav file to Resources. For example you have sound file named Sound.wav. After you added the Sound.wav file to Resources, you can use this code:
System.Media.SoundPlayer player = new System.Media.SoundPlayer(Properties.Resources.Sound);
player.Play();
This is another way to play sound.

Categories

Resources