Media Player control in Visual Basic.NET

In this tutorial we're going to focus on using Windows Media Player in Visual Basic .Net where user is able to play music, videos and viewing of different formats of pictures. Basically the Windows Media Player does not exist in the components portion of the toolbox. So to add the Windows Media Player we need to add it in the toolbox. The first step would be, go to the toolbox and Right click then select Choose Items and the Customize Toolbox dialog box will open. Then Select Windows Media Player on the COM Components. Then click ok. After this step the Windows Media Player control will appear on the current tab. After this process we can add now Windows Media Player to our Form and the default name of this control is “AxWindowsMediaPlayer1”. And these can be changed according to what we wanted for example “myPlayer” so that it could be more easily to read and remember. The next process is that we're going to add other controls to our form such as Listbox, FolderBrowserDialog, MenuStrip and StatusStrip. Plan the objects and Properties
Object	                Property	 Settings
Form1	                Name	         mainFrm
	                Text	         Personal Media Player
	                StartPosition	 CenterScreen
	                ControlBox	 False
AxWindowsMediaPlayer1	Name	         myPlayer
Listbox          	Name	         List
MenuStrip1	        Name	         MenuStrip1
StatusStrip1	        Name	         StatusStrip1
FolderBrowserDialog1	Name	         FolderBrowserDialog1
On the MenuStrip1 we need to add two main menus such Libraries and View. The Libraries have also submenus like Music, Videos, Images and Exit. And for the View submenu is only Playlist Editor. This should look like as shown below. And the final design looks like as shown below. After designing our user interface let’s proceed in adding functionalities to our program. First step double click the main form or we have name it into “mainFrm” to shift our view Designer into view Code. Then on the mainFrm_Load add this code.
  1. list.Items.Clear() ' clear all currect content of the list
  2. list.Hide() ' it will hide the on the main form
  3. myPlayer.Width = 787 ' it will resize the width of myPlayer into 787
and on below of our Public Class mainFrm add this declaration of variable that will hold full path of our folder. And it will like this.
  1. Public Class mainFrm
  2. Dim folderpath As String
After adding this code we’re going create a sub procedure that we will be using it for our program later.
  1. Public Sub jokenresult()
  2. If list.Items.Count > 0 Then
  3. list.Show()
  4. myPlayer.Width = 577
  5. statresult.Text = list.Items.Count & " Items"
  6. Else
  7. list.Hide()
  8. myPlayer.Width = 787
  9. End If
  10. End Sub
Next we will add functionality to the one sub menu items under libraries the Music. To do this just simply double click the Music sub menu. Then you will be redirected to source code view and add this code so it should now look like as shown below.
  1. Private Sub MusicToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MusicToolStripMenuItem.Click
  2. Try
  3. 'it will open the folder dialog where you can select where is the specific folder of your music
  4. FolderBrowserDialog1.ShowDialog()
  5.  
  6. If DialogResult.OK Then
  7. 'if true that if you click ok on the folder dialog box then
  8. 'it will get the selected path of your folder and store it into di variable
  9. Dim di As New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
  10. 'in this line of code it will get all the specific file that has the .mp3 extension and store it into diar1 variable
  11. Dim diar1 As IO.FileInfo() = di.GetFiles("*.mp3")
  12. Dim dra As IO.FileInfo
  13. 'and in this line it will gather all information with regardsto fullpath and names of all files and store it to the folderpath variable
  14. folderpath = di.FullName.ToString
  15. list.Items.Clear()
  16. ' list the names of all files in the specified directory
  17.  
  18. For Each dra In diar1
  19. Dim a As Integer = 0
  20. ' a = a + 1
  21. list.Items.Add(dra)
  22. Next
  23. 'it will call the sub procedure jokenresult() to perform some actions
  24. jokenresult()
  25. End If
  26. Catch ex As Exception
  27. 'if errors occur then the program will catch it and send it back to the user.
  28. MsgBox(ex.Message, MsgBoxStyle.Information)
  29. End Try
  30.  
  31. End Sub
And this is the sample running program playing a selected music. And this is the sample running program playing a selected Movie. And finally this is all of the source code.
  1. 'Description: Personal Media Player that enables user to play Music,Video and pictures etc...
  2. 'Author: Joken Villanueva
  3. 'Date Created:March 23, 2011
  4. 'Modified By:
  5. Public Class mainFrm
  6. Dim folderpath As String
  7.  
  8. Private Sub MusicToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MusicToolStripMenuItem.Click
  9. Try
  10. 'it will open the folder dialog where you can select where is the specific folder of your music
  11. FolderBrowserDialog1.ShowDialog()
  12.  
  13. If DialogResult.OK Then
  14. 'if true that if you click ok on the folder dialog box then
  15. 'it will get the selected path of your folder and store it into di variable
  16. Dim di As New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
  17. 'in this line of code it will get all the specific file that has the .mp3 extension and store it into diar1 variable
  18. Dim diar1 As IO.FileInfo() = di.GetFiles("*.mp3")
  19. Dim dra As IO.FileInfo
  20. 'and in this line it will gather all information with regardsto fullpath and names of all files and store it to the folderpath variable
  21. folderpath = di.FullName.ToString
  22. list.Items.Clear()
  23. ' list the names of all files in the specified directory
  24.  
  25. For Each dra In diar1
  26. Dim a As Integer = 0
  27. ' a = a + 1
  28. list.Items.Add(dra)
  29. Next
  30. 'it will call the sub procedure jokenresult() to perform some actions
  31. jokenresult()
  32. End If
  33. Catch ex As Exception
  34. 'if errors occur then the program will catch it and send it back to the user.
  35. MsgBox(ex.Message, MsgBoxStyle.Information)
  36. End Try
  37.  
  38. End Sub
  39. Public Sub jokenresult()
  40. If list.Items.Count > 0 Then
  41. list.Show()
  42. myPlayer.Width = 577
  43.  
  44. statresult.Text = list.Items.Count & " Items"
  45. Else
  46. list.Hide()
  47. myPlayer.Width = 787
  48. End If
  49.  
  50. End Sub
  51. Private Sub list_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles list.SelectedIndexChanged
  52. 'the myPlayer will play or display something from the list based on the user selected item
  53. myPlayer.URL = folderpath & "\" & list.SelectedItem.ToString
  54. End Sub
  55.  
  56. Private Sub VideosToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VideosToolStripMenuItem.Click
  57.  
  58. Try
  59. FolderBrowserDialog1.ShowDialog()
  60. If DialogResult.OK Then
  61.  
  62. Dim di As New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
  63. Dim diar1 As IO.FileInfo() = di.GetFiles("*.*")
  64.  
  65. Dim dra As IO.FileInfo
  66. folderpath = di.FullName.ToString
  67. list.Items.Clear()
  68. For Each dra In diar1
  69. list.Items.Add(dra)
  70. Next
  71. jokenresult()
  72. End If
  73. Catch ex As Exception
  74. MsgBox(ex.Message, MsgBoxStyle.Information)
  75. End Try
  76.  
  77. MsgBox(folderpath)
  78.  
  79. End Sub
  80.  
  81. Private Sub ImagesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImagesToolStripMenuItem.Click
  82. Try
  83. FolderBrowserDialog1.ShowDialog()
  84. If DialogResult.OK Then
  85.  
  86. Dim di As New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
  87. Dim diar1 As IO.FileInfo() = di.GetFiles("*.jpg")
  88. Dim dra As IO.FileInfo
  89. folderpath = di.FullName.ToString
  90. list.Items.Clear()
  91.  
  92. For Each dra In diar1
  93. list.Items.Add(dra)
  94. Next
  95. jokenresult()
  96. End If
  97. Catch ex As Exception
  98. MsgBox(ex.Message, MsgBoxStyle.Information)
  99. End Try
  100.  
  101.  
  102. End Sub
  103.  
  104.  
  105. Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
  106. Me.Close()
  107.  
  108. End Sub
  109.  
  110. Private Sub PlaylistEditorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlaylistEditorToolStripMenuItem.Click
  111. 'in this line if the playlist editor is click then the list will sho on the form.
  112. If PlaylistEditorToolStripMenuItem.Checked = True Then
  113. list.Show()
  114. myPlayer.Width = 577
  115. Else
  116.  
  117. list.Hide()
  118. myPlayer.Width = 787
  119.  
  120. End If
  121. End Sub
  122.  
  123. Private Sub mainFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  124. list.Items.Clear() ' clear all currect content of the list
  125. list.Hide() ' it will hide the on the main form
  126. myPlayer.Width = 787 ' it will resize the width of myPlayer into 787
  127. End Sub
  128. End Class

Comments

Submitted byarijit ghosh (not verified)on Sat, 07/05/2014 - 17:58

not bad this program

Add new comment