Tag Archives: playback

How-to Playback Movies and Videos with VB and Visual Basic .NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (25 votes, average: 4.12 out of 5)
 

As you will see, playing videos is easy using the Windows MCI Command interface for the playback which is supported in Windows XP, Windows Vista, Windows 7, Windows 8, and Windows 10. Specifically the MCISendString function will be used in this tutorial.

MCI should by default already support the common formats like avi, mpg, mpeg, wmv, etc. with a basic Windows installation. But you can actually get MCI to playback nearly all formats if the correct Codec/Driver is installed. For Example, you can playback Apple QuickTime .Mov, MPEG 4 DivX, or H.264 .MP4 by simply installing the codec for each format. After that just program the device like you normally would.

This article is only to show how to do basic playback of videos and not features like duration/length, position, or resizing the video. You can check out my MCI MultiMedia Tutorial to learn how to program MCI and add your own features.

If your using VB 6.0 select the correct MCISendString declare below, change any IntPtr.Zero code to 0, and use ‘.hWnd’ instead of ‘.Handle’.

Edit: 2014, December 17th: I decided to make a download available that uses all of the codes in this article with a couple extras to help resizing and such. I also changed some of the code in the program for Option Explicit.

Download Example: Click Here

View Screenshot: Click Here


Visual Basic 6.0 and Older

    'The Main API call that will be used for the playback.
    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

 

Visual Basic.NET and Newer

    'The Main API call that will be used for the playback.
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal _
        lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As _
            Integer, ByVal hwndCallback As IntPtr) As Integer

 

OR You Can Use…

    'The main API call.
    <DllImport("winmm.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Shared Function mciSendString(commandString As String, returnString As String, _
        returnStringLength As Integer, _mciCallback As IntPtr) As Int32
    End Function

Now make a couple variables.

    'Will hold the path to the movie file.
    Dim filename As String

    'Holds the return value of mciSendstring. Not used for anything in this article though.
    Dim retVal As Integer

 

Then set a path to the file you want to play. If you’ve seen my other MCI articles you should remember about adding “Quotes” around the path and filename, or at least supply the file’s short pathname.

        'The path to the movie or video to play.
        filename = "c:\movies\MyMovieFile.mp4"

        'Now add the quotes around the path.
        filename = Chr(34) & filename & Chr(34)

Next thing to do is open and setup a playback device. I usually use the MPEGVideo device since it works with audio and video. The MPEGVideo device is DirectShow down under.

There are a couple ways you can open the video. You can have it display in its own popup window or have it displayed in a control like a Panel or Frame. Basically any control with a .hWnd or .Handle property.

This command will display open a new device and have the video display in a control window.

'This code below will open a new mpegvideo device and play the movie in the “movieWindow” control which is
'nothing more than a GroupBox/Frame control I used in an example app. Basically any control with a
'handle can be used. For VB 6 you would use the controls .hWnd property instead of the
'Handle.ToInt32 property.
'If you want MCI to select which device to use just take out "type mpegvideo" from the code.

retVal = mciSendString("open " & filename & " type mpegvideo alias myMovie parent " _
& movieWindow.Handle.ToInt32 & " style child", vbNullString, 0, IntPtr.Zero)

 

If you want the video to open in a separate window use this code instead.

'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 its Own window and the alias name will be “myMovie”.
If you want MCI to select which device to use just take out "type mpegvideo" from the code.

retVal = mciSendString("open " & filename & " type mpegvideo alias myMovie", vbNullString, 0, IntPtr.Zero)

OK, now that you’ve opened a device, all you have to do is call the Play command to begin playback. The codes below will Play, Stop, Pause, and Resume.

Play

 '
 'Start Playing the movie once you’ve setup the device with your file.
 retVal = mciSendString("play myMovie", vbNullString, 0, IntPtr.Zero)

 

Stop


'Will Stop the playback if its currently playing.
 retVal = mciSendString("stop myMovie", vbNullString, 0, IntPtr.Zero)

 

Pause


'Will Pause the playback if its playing.
 retVal = mciSendString("pause myMovie", vbNullString, 0, IntPtr.Zero)

 

Resume


'Will Resume the playback if it has been paused.
retVal = mciSendString("resume myMovie", vbNullString, 0, IntPtr.Zero)

 

You want to use this code when your done with your device and/or closing your program so the mci resources will be cleaned up. You could change ‘close myMovie’ and put ‘close all’ to free everything MCI has used.

        'Will make sure the previous alias is destroyed. If the alias “myMovie” 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 myMmovie", vbNullString, 0, IntPtr.Zero)

Well, that’s all there is to it. Hope you found something useful from this article. Have Fun!

Jason

Revised: 2015

Readymade Options for Playback of Music/Audio Files in VB and VB.NET – 1 of 2

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (10 votes, average: 3.90 out of 5)
 

This article and Part Two are to show some options available for Audio/Music playback if your not interested in making your own control or library. I do have an article on how to do basic music playback at this link with some quick and simple source code. But if you want more advanced features and don’t want to create the bulk of the playback code, then the content in these articles should help you out. This first article is on using some libraries I made around the MCISendString Windows API function and doesn’t have 3rd party dependency.

Note: You can now check out part two at this link on two 3rd party controls named Bass.dll and FMod.dll.


Visual Basic 6.0

For VB 6.0 is Music Module v2.2 that I made with Visual Basic 6.0 and contains the main features that a music player usually needs. It supports the most popular music formats like Wave, MIDI, and MP3 files. Some other features are calculating the MP3’s overall Bitrate, getting the Playing Position in various formats, Left/Right Volume Control with Muting, etc. While the class module is old, it still does the job well and does not rely on any 3rd Party .DLL’s or Resources. You can download the Class Module with Example at this link.

Visual Basic.NET 2002/2003

This option is basically for VB.NET 2002/2003 and would recommend you use it for those versions only. This library is called csMusicLibrary v1.0 Professional. This class contains more features than the VB 6 library.  The class has all of the features required for a normal music player and some extra features like basic playlist support, setting the music playback rate, setting the playing position and more. IF your interested in this option you can download the compiled class with example at this link.

Visual Basic 2005/2008/2010, and Higher

This next library definitely has the most features out of the options in this article. It is a major overhaul of the original csMusicLibrary. It is csMusicLibrary v3.1 – The Professional Series and made with Visual Basic 2005. It is completely XML commented with explanations about the Subs/Functions and Properties along with remarks. There are many options available and is easy to use and understand. Besides your normal playback features it can give your applications some basic recording capabilities, extract MP3 Tag v1.1 information, get the music playback status, and much more. Like the other options outlined above, this library does not rely on any 3rd party controls or resources. If you don’t need any super advanced features like visualizations, data manipulation, or sound effects, etc. then this library should work just fine for making a fully featured music application. You can download the compiled class with example at this link.


All these options should be fine for making a nice featured application. But you may be wanting more advanced features like FFT’s, Data Manipulation, Sound FX, and so on. That is where the next article will come in. Click this link to read part two about some third party controls. Have fun 🙂

Jason

Revised: 2014

Easily Play, Stop, Pause, and Resume MP3/Wav/WMA files in VB and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (31 votes, average: 3.42 out of 5)
 

This article will use the already established Windows API function MCISendString. The function is part of the Windows Multi-Media system in the winmm.dll using Multimedia Command Strings (MCI). WinMM is available on pretty much every modern Windows OS. Apart from having to install special codecs for uncommon audio/music formats, you can play many of the popular formats without any special setup or installation. If you want to know more about Multimedia Command strings you can check out a beginner tutorial I made at this link.

While the source code in this post is in VB.NET, you can simply change ‘as Integer‘ to ‘as Long‘. Otherwise the code should be fine.

Below is the API Function to get started.

'
'Api to send the commands to the mci device.
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

 

The next snippet is for the path to the file.

'The Chr(34) code is to put quotes at the beginning and end of the file's path.
'You can convert the long filename to the short filename and not need 'the quotes.
'That would just be more code that really isn't needed.
Dim fileToPlay As String

fileToPlay = Chr(34) & ("c:\MyFileToPlay.MP3") & Chr(34)

 

Below is a command sent to the MCI system to open and setup a new device to play the file that is specified in the fileToPlay variable.

'Let the command interface decide which device to use. Just specify the alias.
'The alias is the name you use to program that device. You can create multiple
'devices to play media at the same time and such with different alias's.
MCISendString "open " & fileToPlay & " alias myDevice", vbNullString, 0, 0

 

The device is now setup and you just need to execute the simple commands shown below.
Continue reading