Results 1 to 16 of 16

Thread: Video without Windows Media Player in VB.Net form

  1. #1

    Thread Starter
    Addicted Member colossus1958's Avatar
    Join Date
    Oct 2011
    Location
    Florence, KY, USA
    Posts
    208

    Video without Windows Media Player in VB.Net form

    I have tried to find a way to play videos from my form without using WMP (which I am not fond of) and have been unable to do so. Does anyone have an idea on this (DirectShow, perhaps). Also, I am not clear on how ro achieve a reference to DirectX in my form. This might be nice to know, but the information on the net concerning this is a bit muddled. Any information is appreciated (even if you tell me I have to use WMP).
    All it takes for evil to triumph is for good men to stop programming. (My apologies to Edmund Burke)

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,414

    Re: Video without Windows Media Player in VB.Net form


  3. #3

    Thread Starter
    Addicted Member colossus1958's Avatar
    Join Date
    Oct 2011
    Location
    Florence, KY, USA
    Posts
    208

    Re: Video without Windows Media Player in VB.Net form

    I could not get that to work even though it seemed promising. I think some code was missing and since I am unfamiliar with that particular API, I didn't know how to bridge the gap.
    All it takes for evil to triumph is for good men to stop programming. (My apologies to Edmund Burke)

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,414

    Re: Video without Windows Media Player in VB.Net form

    this works. you just need a form with a Panel and 2 Buttons:

    Code:
    Public Class Form1
    
        '
        'The Main API call that will be used for the playback. Simply change the “Integer” to "Long"
        'if your using Classic VB like VB 6.0.
        Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal _
            lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As _
                Integer, ByVal hwndCallback As Integer) As Integer
    
        '
        'Will hold the path to the movie file.
        Dim filename As String = "C:\folder\file.avi"
    
        '
        'Holds the return value of mciSendstring. Not used for anything in this article though.
        Dim retVal As Integer
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ''
            ''Now add the quotes around the path.
            filename = Chr(34) & filename & Chr(34)
    
            '
            'Specify the mpegvideo driver to play the movies which should play most movie formats without any problems.
            'This code will have the video open in Panel1 and the alias name will be “movie”.
            '
            retVal = mciSendString("open " & filename & " type mpegvideo alias movie parent " _
                & Panel1.Handle.ToInt32 & " style child", "", 0, 0)
    
            'Start Playing the movie once you’ve setup the device with your file.
            retVal = mciSendString("play movie", "", 0, 0)
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            'Will Stop the playback if its currently playing.
            retVal = mciSendString("stop movie", "", 0, 0)
    
        End Sub
    
        Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            'Will make sure the previous alias is destroyed. If the alias “movie” hasn’t been created yet,
            'this code will NOT cause any errors or anything. So there is no need to worry about that.
            retVal = mciSendString("close movie", "", 0, 0)
        End Sub
    
    End Class

  5. #5

    Thread Starter
    Addicted Member colossus1958's Avatar
    Join Date
    Oct 2011
    Location
    Florence, KY, USA
    Posts
    208

    Re: Video without Windows Media Player in VB.Net form

    @.paul.: That worked perfectly (almost). The movie was "playing". I could hear the sound. But no video was showing on the panel. I followed your code to the letter only substituting an avi file I had readily available. Did I miss a reference or something?
    All it takes for evil to triumph is for good men to stop programming. (My apologies to Edmund Burke)

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,414

    Re: Video without Windows Media Player in VB.Net form

    You didn't miss a reference...
    The code uses a panel as a screen. Are you sure it wasn't obscured?

  7. #7

    Thread Starter
    Addicted Member colossus1958's Avatar
    Join Date
    Oct 2011
    Location
    Florence, KY, USA
    Posts
    208

    Re: Video without Windows Media Player in VB.Net form

    Nope/ the panel was placed on the left side of the form with the two buttons to the right. Nothing obscured. I will check all of the panel's properties to see if I missed something. I doubt it as all I did was place it on the form and resize it.
    All it takes for evil to triumph is for good men to stop programming. (My apologies to Edmund Burke)

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,123

    Re: Video without Windows Media Player in VB.Net form

    The code worked fine for me.
    I did set the border of the panel to fixed single so I could verify it was there and what its size was.
    I also anchored the panel Top,Bottom,Left,Right so that it would expand with the form, as the video I was looking at (a .wmv file) was large so didn't see much action in the panel at first since it was just the upper left portion of the video (the video doesn't scale with the size of the panel).
    The buttons were just anchored Top,Right so they moved with the form resize and didn't get covered by the panel, or under the panel.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,414

    Re: Video without Windows Media Player in VB.Net form

    it is scaleable:

    Code:
    Private Sub SizeVideoWindow(maxSize as size)
    
        Dim ActualMovieSize As Size = getDefaultSize()
        Dim AspectRatio As Single = ActualMovieSize.Width / ActualMovieSize.Height
    
        Dim iLeft As Integer = 0
        Dim iTop As Integer = 0
    
        Dim newWidth As Integer = maxSize.width
        Dim newHeight As Integer = newWidth \ AspectRatio
    
        If newHeight > maxSize.height Then
    
            newHeight = maxSize.height
            newWidth = newHeight * AspectRatio
            iLeft = (maxSize.width - newWidth) \ 2
    
        Else
    
            iTop = (maxSize.height - newHeight) \ 2
    
        End If
    
        mciSendString("put movie window at " & iLeft & " " & iTop & " " & newWidth & " " & newHeight, 0, 0, 0)
    
    End Sub
    
    Public Function getDefaultSize() As Size
        'Returns the default width, height the movie
        Dim c_Data As String = Space(128)
        mciSendString("where movie source", c_Data, 128, 0)
        Dim parts() As String = Split(c_Data, " ")
    
        Return New Size(CInt(parts(2)), CInt(parts(3)))
    End Function

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,414

    Re: Video without Windows Media Player in VB.Net form

    here it is with scaled video:

    Code:
    Public Class Form1
    
        '
        'The Main API call that will be used for the playback. Simply change the “Integer” to "Long"
        'if your using Classic VB like VB 6.0.
        Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal _
            lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As _
                Integer, ByVal hwndCallback As Integer) As Integer
    
        '
        'Will hold the path to the movie file.
        Dim filename As String = "C:\folder\file.avi"
    
        '
        'Holds the return value of mciSendstring. Not used for anything in this article though.
        Dim retVal As Integer
    
        Dim playing As Boolean = False
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ''
            ''Now add the quotes around the path.
            filename = Chr(34) & filename & Chr(34)
    
            '
            'Specify the mpegvideo driver to play the movies which should play most movie formats without any problems.
            'This code will have the video open in Panel1 and the alias name will be “movie”.
            '
            retVal = mciSendString("open " & filename & " type mpegvideo alias movie parent " _
                & Panel1.Handle.ToInt32 & " style child", "", 0, 0)
    
            'Start Playing the movie once you’ve setup the device with your file.
            retVal = mciSendString("play movie", "", 0, 0)
            playing = True
            SizeVideoWindow(Panel1.Size)
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            'Will Stop the playback if its currently playing.
            retVal = mciSendString("stop movie", "", 0, 0)
            retVal = mciSendString("close movie", "", 0, 0)
            playing = False
        End Sub
    
        Private Sub Panel1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.SizeChanged
            If playing Then
                SizeVideoWindow(Panel1.Size)
            End If
        End Sub
    
        Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            'Will make sure the previous alias is destroyed. If the alias “movie” hasn’t been created yet,
            'this code will NOT cause any errors or anything. So there is no need to worry about that.
            retVal = mciSendString("close movie", "", 0, 0)
        End Sub
    
    
        Private Sub SizeVideoWindow(ByVal maxSize As Size)
    
            Dim ActualMovieSize As Size = getDefaultSize()
            Dim AspectRatio As Single = CSng(ActualMovieSize.Width / ActualMovieSize.Height)
    
            Dim iLeft As Integer = 0
            Dim iTop As Integer = 0
    
            Dim newWidth As Integer = maxSize.width
            Dim newHeight As Integer = CInt(newWidth / AspectRatio)
    
            If newHeight > maxSize.height Then
    
                newHeight = maxSize.height
                newWidth = CInt(newHeight * AspectRatio)
                iLeft = (maxSize.width - newWidth) \ 2
    
            Else
    
                iTop = (maxSize.height - newHeight) \ 2
    
            End If
    
            mciSendString("put movie window at " & iLeft & " " & iTop & " " & newWidth & " " & newHeight, "", 0, 0)
    
        End Sub
    
        Public Function getDefaultSize() As Size
            'Returns the default width, height the movie
            Dim c_Data As String = Space(128)
            mciSendString("where movie source", c_Data, 128, 0)
            Dim parts() As String = Split(c_Data, " ")
    
            Return New Size(CInt(parts(2)), CInt(parts(3)))
        End Function
    
    End Class

  11. #11

    Thread Starter
    Addicted Member colossus1958's Avatar
    Join Date
    Oct 2011
    Location
    Florence, KY, USA
    Posts
    208

    Re: Video without Windows Media Player in VB.Net form

    I tried that last code with the resizing capabilities and still got no video. Worse, I got an error saying "conversion from string "" to integer is not valid". This was on the Return line of the Function getDefaultSize. However, the audio kept on playing until I cancelled the error message and exited the program. There has to be something different about A) my system, or B) the file (movie) I'm using. I am running Visual Studio Express 2013 Update 4 on Windows 8.1. The video file is an avi type.
    All it takes for evil to triumph is for good men to stop programming. (My apologies to Edmund Burke)

  12. #12
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,123

    Re: Video without Windows Media Player in VB.Net form

    Perhaps try another video.
    Do you have the Wildlife.wmv video in the "C:\Users\Public\Videos\Sample Videos" directory. I believe it is present by default on Win7 machines. Don't know about Win8.x

    Oh, I forgot to mention that I did have to set "Option Strict Off" at the top of the file because there are some implicit conversions in the code that are not allowed with Option Strict set.
    You can usually just click on the recommended fix to have the IDE add the required explicit conversion to get rid of the error. I just took the easy way out and added the "Option Strict Off" to see if the "Errors" would go away. There were four implicit conversions not allowed by Option Strict On in the original code, one of which was Integer to String. I haven't tried the newer code yet.

    p.s. I just tried the newer code posted in Post #10, and didn't have to add Option Strict Off, it worked fine "out of the box" for me with Option Strict On.
    I am running VS 2010 pro, so there may be something between VS2013 or Win8.x I don't have that combination anywhere to try.
    Last edited by passel; Dec 15th, 2014 at 03:58 PM.

  13. #13

    Thread Starter
    Addicted Member colossus1958's Avatar
    Join Date
    Oct 2011
    Location
    Florence, KY, USA
    Posts
    208

    Re: Video without Windows Media Player in VB.Net form

    Found the "wildlife" video online and tried it. Still no joy. I am going to show you my code as written to see if you can find the error. I am not understanding why I am getting audio but no video.

    Code:
    Public Class Form1
        '
        'The main API call that will be used to playback the video.
        '
        Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
        '
        'Variable to hold the filepath of the movie.
        '
        Dim filename As String = "C:\Users\Owner\Documents\Wildlife_512kb.mp4"
        '
        'Variable to hold the return value of mciSendString. Not used for anything in this format.
        '
        Dim retVal As Integer
        Dim playing As Boolean = False
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            '
            'Add quotes (") around filepath.
            '
            filename = Chr(34) & filename & Chr(34)
            'Specify the mpegvideo driver to play the movie. This should play most movies
            'without any problems.
            '
            retVal = mciSendString("open " & filename & " type mpegvideo alias movie parent " & Panel1.Handle.ToInt32 & " style child", "", 0, 0)
            '
            'Start playing the movie after setup.
            '
            retVal = mciSendString("play movie", "", 0, 0)
            playing = True
            'SizeVideoWindow(Panel1.Size)
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            '
            'Stops playback if movie is currently playing.(actually it pauses...)
            '
            retVal = mciSendString("stop movie", 0, 0, 0)
            'retVal = mciSendString("close movie", "", 0, 0)
            playing = False
        End Sub
    
        Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
            'Will make sure the previous alias is destroyed. If the alias "movie" hasn't been created
            'yet, this code will not cause errors
            retVal = mciSendString("close movie", "", 0, 0)
        End Sub
    
        'Private Sub Panel1_SizeChanged(sender As Object, e As EventArgs)
        '    If playing Then
        '        SizeVideoWindow(Panel1.Size)
        '    End If
        'End Sub
    
        'Private Sub SizeVideoWindow(ByVal maxSize As Size)
        '    Dim ActualMovieSize As Size = getDefaultSize()
        '    Dim AspectRatio As Single = CSng(ActualMovieSize.Width / ActualMovieSize.Height)
        '    Dim iLeft As Integer = 0
        '    Dim iTop As Integer = 0
        '    Dim newWidth As Integer = maxSize.Width
        '    Dim newHeight As Integer = CInt(newWidth / AspectRatio)
    
        '    If newHeight > maxSize.Height Then
        '        newHeight = maxSize.Height
        '        newWidth = CInt(newHeight * AspectRatio)
        '        iLeft = (maxSize.Width - newWidth) \ 2
        '    Else
        '        iTop = (maxSize.Height - newHeight) \ 2
        '    End If
    
        '    mciSendString("put movie window at " & iLeft & " " & iTop & " " & newWidth & " " & newHeight, "", 0, 0)
    
        'End Sub
    
        'Public Function getDefaultSize() As Size
        '    'Returns the default width and height of the movie.
        '    Dim c_Data As String = Space(128)
        '    mciSendString("where movie source", c_Data, 128, 0)
        '    Dim parts() As String = Split(c_Data, " ")
    
        '    Return New Size(DirectCast((parts(2)), Integer), DirectCast((parts(3)), Integer))
    
        'End Function
    
    End Class
    All it takes for evil to triumph is for good men to stop programming. (My apologies to Edmund Burke)

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,414

    Re: Video without Windows Media Player in VB.Net form

    I've never tried this api with win 8.1, so I can't suggest a solution

  15. #15

    Thread Starter
    Addicted Member colossus1958's Avatar
    Join Date
    Oct 2011
    Location
    Florence, KY, USA
    Posts
    208

    Re: Video without Windows Media Player in VB.Net form

    Well the only answer I can think of at this time is to try another computer. I have a laptop which still has XP on it (believe it or not). I'll try out the program on that and see if I can get it to work there. If so, I will let you (and Microsoft) know.
    All it takes for evil to triumph is for good men to stop programming. (My apologies to Edmund Burke)

  16. #16

    Thread Starter
    Addicted Member colossus1958's Avatar
    Join Date
    Oct 2011
    Location
    Florence, KY, USA
    Posts
    208

    Re: Video without Windows Media Player in VB.Net form

    Well, that didn't work either. Guess I'll have to ask Microsoft if Win 8.1 is the problem. I mean maybe something in the code has to change for WIN8 to use it properly. If I find out I will let everybody know.
    All it takes for evil to triumph is for good men to stop programming. (My apologies to Edmund Burke)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width