SCProject.Biz CODER logoPlaying Multimedia Files




Wave Player:


In VB.Net you can play PCM wave files with the System.Media.SoundPlayer.
Here's the example code:


Public Class frmDemo

    'wav player class
    Dim player As New Media.SoundPlayer

    Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
        Dim ofd As New OpenFileDialog With { _
        .Title = "Select Music File", _
        .Filter = "WAV Files (*.wav)|*.wav", _
        .FilterIndex = 0, _
        .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)}
        'show the dialog + play the selected WAV unless cancelled
        'or the selected WAV isn't a PCM wave file
        If ofd.ShowDialog = DialogResult.OK Then
            Try
                player.SoundLocation = ofd.FileName
                player.Play()
                btnsEnableDisable(False, True)
            Catch ex As Exception
                MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Try
        End If
    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        'stop the player + Enable/Disable btns
        player.Stop()
        btnsEnableDisable(True, False)
    End Sub

    Private Sub btnsEnableDisable(ByVal b1 As Boolean, ByVal b2 As Boolean)
        'Enable/Disable all btns
        btnPlay.Enabled = b1
        btnStop.Enabled = b2
    End Sub

End Class

You can download the example project here: WavPlayer.zip



Other music files can be played with the mciSendString API function.


Music Player:


Here's the example code:


Public Class frmDemo

    '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

    Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
        Dim ofd As New OpenFileDialog With { _
        .Title = "Select Music File", _
        .Filter = "MP3 Files (*.mp3)|*.mp3|WAV Files (*.wav)|*.wav|WMA Files (*.wma)|*.wma", _
        .FilterIndex = 0, _
        .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)}
        'show the dialog + play the selected Music File unless cancelled
        If ofd.ShowDialog = DialogResult.OK Then
            Dim _fileToPlay As String = Chr(34) + ofd.FileName + Chr(34)
            mciSendString("open " & _fileToPlay & " alias myDevice", Nothing, 0, 0)
            mciSendString("play myDevice", Nothing, 0, 0)
            btnsEnableDisable(False, True, False, True)
        End If
    End Sub

    Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click
        'pause + Enable/Disable btns
        mciSendString("pause myDevice", Nothing, 0, 0)
        btnsEnableDisable(False, False, True, True)
    End Sub

    Private Sub btnResume_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResume.Click
        'resume + Enable/Disable btns
        mciSendString("resume myDevice", Nothing, 0, 0)
        btnsEnableDisable(False, True, False, True)
    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        'stop + close + Enable/Disable btns
        mciSendString("stop myDevice", Nothing, 0, 0)
        mciSendString("close myDevice", Nothing, 0, 0)
        btnsEnableDisable(True, False, False, False)
    End Sub

    Private Sub btnsEnableDisable(ByVal b1 As Boolean, ByVal b2 As Boolean, ByVal b3 As Boolean, ByVal b4 As Boolean)
        'Enable/Disable all btns
        btnPlay.Enabled = b1
        btnPause.Enabled = b2
        btnResume.Enabled = b3
        btnStop.Enabled = b4
    End Sub

End Class            

You can download the example project here: sounds player.zip

Movie Player:


This example uses DirectX + the WindowsMediaPlayer control.



VB.Net Movie Player



Here's the example code for the main Form:


Imports Microsoft.DirectX.AudioVideoPlayback

Public Class frmVideoInfo

    'VideoInfo Structure
    'holds all information about 1 movie
    Private Structure VideoInfo
        Dim videoName As String
        Dim videoWidth As Integer
        Dim videoHeight As Integer
        Dim videoFps As Double
        Dim videoDuration As String
        'returns a string array for easily adding details to the listview
        Public Function toStringArray(ByVal pb As ProgressBar) As String()
            pb.PerformStep()
            Return New String() {Me.videoName, String.Format("{0}Wx{1}H", Me.videoWidth, Me.videoHeight), Me.videoDuration, Me.videoFps.ToString("f1")}
        End Function
    End Structure

    'create + return a new VideoInfo
    Private Function getVideoInfo(ByVal fi As IO.FileInfo) As VideoInfo
        Dim videoFile As New Video(fi.FullName)
        Dim info As New VideoInfo
        info.videoName = fi.Name
        info.videoWidth = videoFile.Size.Width
        info.videoHeight = videoFile.Size.Height
        info.videoFps = 1 / videoFile.AverageTimePerFrame
        Dim ts As TimeSpan = TimeSpan.FromSeconds(videoFile.Duration)
        info.videoDuration = String.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds)
        Return info
    End Function

    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        'create + show a new FolderBrowserDialog
        Dim fbd As New FolderBrowserDialog
        fbd.SelectedPath = IO.Path.Combine(IO.Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)).FullName, "Videos")
        If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
            'if not cancelled, get all avi, mpg, + wmv files from SelectedPath
            Dim di As New IO.DirectoryInfo(fbd.SelectedPath)
            Dim files As New List(Of IO.FileInfo)(di.GetFiles("*.avi"))
            files.AddRange(di.GetFiles("*.mpg"))
            files.AddRange(di.GetFiles("*.wmv"))
            'set progressbar max
            pbLoading.Maximum = files.Count
            'convert files into detailed listviewitems + add items to listview
            Dim items() As ListViewItem = Array.ConvertAll(files.ToArray, Function(fi) New ListViewItem(getVideoInfo(fi).toStringArray(pbLoading)) With {.Tag = fi.FullName})
            lvDetails.Items.AddRange(items)
            'clear progressbar
            pbLoading.Value = 0
        End If
    End Sub

    Private Sub lvDetails_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvDetails.MouseDoubleClick
        'open frmPlayer + play the doubleclicked item
        Dim frm As New frmPlayer
        frm.moviePlayer.URL = lvDetails.HitTest(e.Location).Item.Tag.ToString
        frm.ShowDialog()
    End Sub

End Class         

This is the viewer Form + code:


VB.Net Movie Player



Public Class frmPlayer

    Private Sub frmPlayer_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        'stop movie on form.close
        moviePlayer.Ctlcontrols.stop()
    End Sub

End Class        

You can download the example project here: Movie Player.zip