A member asked in our C# Facebook group about how can he implement a Video player in C# windows forms. So here I’ve wrote the complete tutorial.
Get the latest Media Player downloads from the Official Microsoft Download Center. C# - Windows Forms - Media Player. Ask Question Asked 5 years, 4 months ago. Windows Media Player is embedded without controls, and with only the.
1- You have to include Microsoft.DirectX.AudioVideoPlayback.dll in your Project.
>> [check how to attach a dll in csharp windows form?]2- Now add this Assembly to your project.
We are adding above code in our app.config file because sometimes the following error occurs in our application.
This error is due to .Net version compatibility. It can be resolved by adding above code in your app.config file.
4- And the work is done. All you have to do is see this code & if you need any more functions in this add them.
Download this source code with complete windows forms & all files.
You can use this snippet to stop the windows music, yes, if you use the following code you'll be able to stop (or jump to next and previous track) the music of windows. That means that any active music player we'll respond to the action. This is possible thanks to the simulation of a keypress of the system using the user32.dll and the keybd_event
function.
To start, we need to include the following namespace in the main class of our project.
Then we are going to import into our project the user32.dll , this api will allow us to get acces to the keybd_event function which we will use to simulate a couple of key press in the system. Immediately, create the method too using :
Now we'll be able to simulate the key events that we need to play-pause-JumpToNextTrack or JumpToPreviousTrack, but we need to declarate too the in variables the codes of the keys that we want to simulate. Just add the following variables in the class (note that they need to be accesible for all the methods):
To simulate events, use the following lines of code (everyline does something different, captain obvious) :
The keybd_event
function expects as first parameter the code of the key that will be simulated. Now you just need to add a couple of buttons to your form and attach those functions to click events of the buttons.
A final implementation should work like a charm, the following gif shows the app working with Spotify music. A you may know this app doesn't have any spotify code or anything else, it will only simulate a keyboard event on the system. Therefore this function will work for any music player for windows.
Your class should look like the following when you add the buttons :
Have fun !