ads_header

How To Play Video In Visual Studio


How To Play Video In Visual Studio





Preview



Hello guys, in this post I will learn a bit about how to process video files in Visual Studio. That is making a video player in a form that comes with the control buttons like play, pause and more.

If so, we just start the tutorial. As always create a new project and prepare a video that you will play.

When ready, now start designing the form design. more or less like the picture below


NOTES :

The components we need:
  • 8 Buttons
  • 1 Textbox
  • 1 OpenFileDialog
  • 1 Windows Media Player
If the Windows Media Player component is not found in Toolbox, you need to add it first, by right-clicking one part of the Toolbox as an example in All Windows Forms and then selecting Choose Items as shown below


This will bring up the Choose Toolbox Items dialog box, and click the COM Components tab and Scroll down until you find Windows Media Player then check it, then click OK


Windows Media Player automatically is already in the Toolbox section All Windows Forms then drag into the form as in the previous picture.





Now double-click Browse Button and enter the following code to search for the video to be played using OpenFileDialog

Private Sub ButtonBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBrowse.Click   
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.Filter = "Video File | *.mp4; *.mpg;"
        OpenFileDialog1.InitialDirectory = "E:\"
        OpenFileDialog1.ShowDialog()

        If OpenFileDialog1.FileName = "" Then
            Exit Sub
        Else
            TextBox1.Text = OpenFileDialog1.FileName.ToString
            AxWindowsMediaPlayer1.URL = TextBox1.Text
        End If
End Sub


Next we enter the code for the control buttons below

Private Sub ButtonPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPlay.Click
     AxWindowsMediaPlayer1.Ctlcontrols.play()
End Sub

Private Sub ButtonPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPause.Click
     AxWindowsMediaPlayer1.Ctlcontrols.pause()
End Sub

Private Sub ButtonStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStop.Click
     AxWindowsMediaPlayer1.Ctlcontrols.stop()
End Sub

Private Sub ButtonMute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonMute.Click
     If ButtonMute.Text = "MUTE" Then
         AxWindowsMediaPlayer1.settings.mute = True
         ButtonMute.Text = "VOICE"
     Else
         AxWindowsMediaPlayer1.settings.mute = False
         ButtonMute.Text = "MUTE"
     End If
End Sub

Private Sub ButtonSlow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSlow.Click
     AxWindowsMediaPlayer1.settings.rate = 0.5
End Sub

Private Sub ButtonNormal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNormal.Click   
     AxWindowsMediaPlayer1.settings.rate = 1
End Sub

Private Sub ButtonFast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonFast.Click
     AxWindowsMediaPlayer1.settings.rate = 1.5
End Sub

NOTES :
  • ButtonPlay : Playing a video
  • ButtonPause : Pause the video
  • ButtonStop : Stop the video
  • ButtonMute : If the text button "MUTE" then the video does not sound if "VOICE" then the video back sound.
  • ButtonSlow : Playing video at a speed of 0.5 (Slow)
  • ButtonNormal : Playing video at a speed of 1 (Normal)
  • ButtonFast : Playing video at a speed of 1.5 (Fast)
    Up here, the program is ready to run.

    For more details, please watch the video below


    No comments:

    Powered by Blogger.