I am making a program and wanted to play a video when I press the one of the buttons, but I have no ideia how to make this, I wanted to play the video and display a number that is going to be randomly generated
I tried using the System.Media library but I couldn't find a player for video, just for sounds
CodePudding user response:
If you're just looking for a player component for WinForms, check out the Windows Media Player Control.
If you need more information specific to implementation, you shoulkd take a look at this article.
CodePudding user response:
In order to play a video file in a .NET program, you will need to use a media player control. There are several options available for this purpose, including:
Windows Media Player ActiveX control: This is a built-in control that is available in the .NET framework. You can add it to your program by dragging it from the toolbox onto a form in your project. You can then use the URL property of the control to specify the path to the video file, and the Ctlcontrols property to control playback (e.g. Ctlcontrols.play() to start playing the video). MediaElement control: This is a control that is included in the WPF framework, which allows you to play audio and video files in a .NET program. You can use the Source property of the control to specify the path to the video file, and the Play method to start playback. Third-party media player controls: There are also several third-party media player controls available that you can use in your .NET program. Some popular options include VLC player and the Infragistics NetAdvantage MediaPlayer. To display a randomly generated number while the video is playing, you can use a label control or a text box control to display the number. You can then use the Random class to generate a random number and set the text of the control to that number.
This is an example of how you might do this in a WPF program using a MediaElement control:
// Create a random number generator
Random rnd = new Random();
// Set the source of the MediaElement to the path of the video file mediaElement.Source = new Uri("path/to/video.mp4", UriKind.Relative);
// Start playback of the video mediaElement.Play();
// Update the label with a randomly generated number every second while (mediaElement.IsPlaying) { int num = rnd.Next(1, 100); // generate a random number between 1 and 100 label.Content = num; // set the text of the label to the random number await Task.Delay(1000); // wait one second }