Results 1 to 16 of 16

Thread: [RESOLVED] Play video (mpg/avi/mp4) in PictureBox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2020
    Posts
    26

    Resolved [RESOLVED] Play video (mpg/avi/mp4) in PictureBox

    I don't post often but when I have someone always comes through with the answer! This seems like it should be simple but I'm stumped on this one.

    I'm trying to play a video file in a PictureBox. I need to be able to supply the routine with the video file name and then play the video and repeat (play and then when it's over start playing again) until it's told to cancel playing. I'm also needing it to stretch / shrink to the size of the picturebox.

    I've found lots of little code pieces on here and on the web but have not been able to get any of them to work for me.

    Can anyone help me out with a routine that works (maybe using mciSendString)?

    Thanks!

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,733

    Re: Play video (mpg/avi/mp4) in PictureBox

    You can't play a video in a picturebox control.
    Check this thread for playing a video using other components:
    https://www.vbforums.com/showthread....-Play-MP4-file

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,261

    Re: Play video (mpg/avi/mp4) in PictureBox

    Quote Originally Posted by Arnoutdv View Post
    You can't play a video in a picturebox control.
    Whoever said that?
    In the end a video consists of individual (Picture-) Frames.
    You'd have to process all frames of a video, convert them individually to a BMP, display it in the PictureBox.
    And now i've not even gone into I-Frames, P-Frames and B-Frames

    That said: It's possible, but YOU DON'T WANT to play a video in a PictureBox.
    Use a ready made component
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    Hyperactive Member -Franky-'s Avatar
    Join Date
    Dec 2022
    Location
    Bremen Germany
    Posts
    476

    Re: Play video (mpg/avi/mp4) in PictureBox

    I would use the MediaFoundation for this. You simply specify a window handle on which the video should be played.

  5. #5
    The Idiot
    Join Date
    Dec 2014
    Posts
    3,001

    Re: Play video (mpg/avi/mp4) in PictureBox

    picturebox is a good surface for a video-player.
    you can use media foundation. do a search.
    I discussed this with -Franky- u can check my post in directsound & Winmm
    where he linked to https://www.activevb.de/cgi-bin/upload/upload.pl
    check his uploads and u should find a media foundation example.

  6. #6
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,653

    Re: Play video (mpg/avi/mp4) in PictureBox

    Generally a PictureBox is convenient as a container with a HWND and HDC, for hosting rendering frames for something like Media Foundation objects or DirectX rendering.

    A ready made component, like in the Windows media subsystem?

    OP was on the right track, it's possible with mciSendString. I've modified this example by .NetNinja from VB.NET to work in VB6.

    Code:
        Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
        Const WS_CHILD As Long = &H40000000
    
        Private Sub PlayMedia(ByRef FileName As String, ByVal Window As PictureBox)
            FileName = Chr(34) & FileName & Chr(34)
            Call mciSendString("Open " & FileName & " alias MediaFile parent " & CStr(Window.hWnd) & " style " & CStr(WS_CHILD), vbNullString, 0, 0)
            Call mciSendString("put MediaFile window at 0 0 " & CStr(Window.ScaleWidth) & " " & CStr(Window.ScaleHeight), vbNullString, 0, 0)
            Call mciSendString("Play MediaFile", vbNullString, 0, 0)
        End Sub
    Called like PlayMedia "C:\path\to\video.avi", Picture1

    Picture1 should have ScaleMode changed to vbPixels, and I don't know if AutoRedraw = True is always needed but I set it when testing this.
    Last edited by fafalone; Sep 13th, 2023 at 11:55 AM.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2020
    Posts
    26

    Re: Play video (mpg/avi/mp4) in PictureBox

    Thank You fafalone!

    I had been working with some code that was very close to that but didn't work. Your example works!

    I also was able to change the line to:
    Call mciSendString("Play MediaFile Repeat", vbNullString, 0, 0)
    Which allowed the video to repeat after it has finished playing and then I used:
    Call mciSendString("Close MediaFile", vbNullString, 0, 0)
    to stop / close the video.

    Seems to work fine with a AVI file - might need to install some codecs to get it to work with other files.

    Thank you so much! I knew someone on here would have the answer!

  8. #8
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] Play video (mpg/avi/mp4) in PictureBox

    Be careful to test this.

    A lot of legacy multimedia stuff is available in Windows 11 but is not installed by default any more.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2020
    Posts
    26

    Re: Play video (mpg/avi/mp4) in PictureBox

    Quote Originally Posted by fafalone View Post
    Generally a PictureBox is convenient as a container with a HWND and HDC, for hosting rendering frames for something like Media Foundation objects or DirectX rendering.
    Do you know of anyway to have any labels / command buttons, controls in general that are placed on the PictureBox to be visible while the video is playing? Even when a form is used as the container for the video all controls are not visible while a video is playing.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2020
    Posts
    26

    Re: [RESOLVED] Play video (mpg/avi/mp4) in PictureBox

    Quote Originally Posted by dilettante View Post
    Be careful to test this.

    A lot of legacy multimedia stuff is available in Windows 11 but is not installed by default any more.
    Thanks dilettante! That's a good point - I'll make sure to test on Windows 11 as well. Do you have a suggestion for a better way to achieve this?

  11. #11
    Hyperactive Member -Franky-'s Avatar
    Join Date
    Dec 2022
    Location
    Bremen Germany
    Posts
    476

    Re: [RESOLVED] Play video (mpg/avi/mp4) in PictureBox

    Quote Originally Posted by Kevin2 View Post
    Do you have a suggestion for a better way to achieve this?
    I can only recommend you take a look at the MediaFoundation. If you only want it from Win10, then the COM interfaces IMFMediaEngine and IMFMediaEngineEx plus two or three other COM interfaces and APIs are enough to build a simple media player. The MediaFoundation also comes with various codecs. A codec would only have to be installed for videos in H265 format and you can get it from the MS Store. Public WebTV and WebRadio streams can also be played using IMFMediaEngine(Ex).

  12. #12
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,653

    Re: [RESOLVED] Play video (mpg/avi/mp4) in PictureBox

    For Media Foundation the MFPCreateMediaPlayer API + IMFPMediaPlayer interface is about as simple as mciSendString. It's pretty ridiculous they marked it deprecated and replaced it with the far more complicated media foundation stuff. Be careful if you need Windows 7 support, some of it requires 8 or 10.

    https://learn.microsoft.com/en-us/wi...ed-with-mfplay

    MF interfaces are defined in oleexp.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Sep 2020
    Posts
    26

    Re: [RESOLVED] Play video (mpg/avi/mp4) in PictureBox

    Quote Originally Posted by -Franky- View Post
    I can only recommend you take a look at the MediaFoundation. If you only want it from Win10, then the COM interfaces IMFMediaEngine and IMFMediaEngineEx plus two or three other COM interfaces and APIs are enough to build a simple media player. The MediaFoundation also comes with various codecs. A codec would only have to be installed for videos in H265 format and you can get it from the MS Store. Public WebTV and WebRadio streams can also be played using IMFMediaEngine(Ex).
    Thanks Franky - I'll have a look. For the most part this program will be deployed on Windows 10 & 11 machines however there are a few installations that will be on Windows 7.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Sep 2020
    Posts
    26

    Re: [RESOLVED] Play video (mpg/avi/mp4) in PictureBox

    Quote Originally Posted by fafalone View Post
    For Media Foundation the MFPCreateMediaPlayer API + IMFPMediaPlayer interface is about as simple as mciSendString. It's pretty ridiculous they marked it deprecated and replaced it with the far more complicated media foundation stuff. Be careful if you need Windows 7 support, some of it requires 8 or 10.

    https://learn.microsoft.com/en-us/wi...ed-with-mfplay

    MF interfaces are defined in oleexp.
    Thanks - I'll have a look.

  15. #15
    Hyperactive Member -Franky-'s Avatar
    Join Date
    Dec 2022
    Location
    Bremen Germany
    Posts
    476

    Re: [RESOLVED] Play video (mpg/avi/mp4) in PictureBox

    From Win7 and higher you can also use other MediaFoundation COM interfaces. These also work on Win10 and higher. I only have a C++ description of how it works: https://learn.microsoft.com/en-us/wi...ed-media-files

  16. #16
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,619

    Wink Re: [RESOLVED] Play video (mpg/avi/mp4) in PictureBox

    Another way to easily play a video file in a PictureBox would be to embed a XAML MediaPlayerElement control and let it do its magic. It has the major advantage of already including a built-in interface for controlling the video playback, volume and other options thus sparing you a lot of nitty-gritty work.

Tags for this Thread

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