ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

How to Make Media Player Using Visual Basic.Net

In this tutorial, i will be teaching you How to create a customize Media Player that is capable of playing sounds,Videos and viewing of images in different format. So let’s get started. But since Media Player does not exist in the component portion of toolbox, we need to add the Windows Media Player to the toolbox. To do this, just follow the given steps.

  1. Go to the toolbox and Right click
  2. Then select Choose Items and the Customize Toolbox dialog box will open.
  3. And select Windows Media Player on the COM Components.
  4. Then click “OK
  5. And finally, 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”. Then you are free to change the name of this object based on what you desire for instance you to name it as “WMPlayer” 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 has also sub menus like Music, Videos, Images and Exit. And for the View sub menu is only Playlist Editor. This should look like as shown below.

submenus

And the final design is look like as shown below.

finaloutput

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.  
  3. list.Hide()             ' it will hide the on the main form
  4.  
  5. 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 later path of our folder. And it will like this.

  1. Public Class mainFrm
  2.  
  3. 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.  
  3. If list.Items.Count > 0 Then
  4.  
  5. list.Show()
  6.  
  7. myPlayer.Width = 577
  8.  
  9. statresult.Text = list.Items.Count & " Items"
  10.  
  11. Else
  12.  
  13. list.Hide()
  14.  
  15. myPlayer.Width = 787
  16.  
  17. End If
  18.  
  19. 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.  
  3. Try
  4.  
  5. 'it will open the folder dialog where you can select where is the specific folder of your music
  6.  
  7. FolderBrowserDialog1.ShowDialog()
  8.  
  9.  
  10.  
  11. If DialogResult.OK Then
  12.  
  13. 'if true that if you click ok on the folder dialog box then
  14.  
  15. 'it will get the selected path of your folder and store it into di variable
  16.  
  17. Dim di As New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
  18.  
  19. 'in this line of code it will get all the specific file that has the .mp3 extension and store it into diar1 variable
  20.  
  21. Dim diar1 As IO.FileInfo() = di.GetFiles("*.mp3")
  22.  
  23. Dim dra As IO.FileInfo
  24.  
  25. 'and in this line it will gather all information with regardsto fullpath and names of all files and store it to the folderpath variable
  26.  
  27. folderpath = di.FullName.ToString
  28.  
  29. list.Items.Clear()
  30.  
  31. ' list the names of all files in the specified directory
  32.  
  33.  
  34.  
  35. For Each dra In diar1
  36.  
  37. Dim a As Integer = 0
  38.  
  39. ' a = a + 1
  40.  
  41. list.Items.Add(dra)
  42.  
  43. Next
  44.  
  45. 'it will call the sub procedure jokenresult() to perform some actions
  46.  
  47. jokenresult()
  48.  
  49. End If
  50.  
  51. Catch ex As Exception
  52.  
  53. 'if errors occur then the program will catch it and send it back to the user.
  54.  
  55. MsgBox(ex.Message, MsgBoxStyle.Information)
  56.  
  57. End Try
  58.  
  59.  
  60.  
  61. End Sub

And this is the sample running program playing a selected music.

playingMusic

 

And this is the sample running program playing a selected Movie.

playingMovie

 

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.  
  3. 'Author:      Joken Villanueva
  4.  
  5. 'Date Created:March 23, 2011
  6.  
  7. 'Modified By:
  8.  
  9. Public Class mainFrm
  10.  
  11. Dim folderpath As String
  12.  
  13.  
  14.  
  15. Private Sub MusicToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MusicToolStripMenuItem.Click
  16.  
  17. Try
  18.  
  19. 'it will open the folder dialog where you can select where is the specific folder of your music
  20.  
  21. FolderBrowserDialog1.ShowDialog()
  22.  
  23.  
  24.  
  25. If DialogResult.OK Then
  26.  
  27. 'if true that if you click ok on the folder dialog box then
  28.  
  29. 'it will get the selected path of your folder and store it into di variable
  30.  
  31. Dim di As New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
  32.  
  33. 'in this line of code it will get all the specific file that has the .mp3 extension and store it into diar1 variable
  34.  
  35. Dim diar1 As IO.FileInfo() = di.GetFiles("*.mp3")
  36.  
  37. Dim dra As IO.FileInfo
  38.  
  39. 'and in this line it will gather all information with regardsto fullpath and names of all files and store it to the folderpath variable
  40.  
  41. folderpath = di.FullName.ToString
  42.  
  43. list.Items.Clear()
  44.  
  45. ' list the names of all files in the specified directory
  46.  
  47.  
  48.  
  49. For Each dra In diar1
  50.  
  51. Dim a As Integer = 0
  52.  
  53. ' a = a + 1
  54.  
  55. list.Items.Add(dra)
  56.  
  57. Next
  58.  
  59. 'it will call the sub procedure jokenresult() to perform some actions
  60.  
  61. jokenresult()
  62.  
  63. End If
  64.  
  65. Catch ex As Exception
  66.  
  67. 'if errors occur then the program will catch it and send it back to the user.
  68.  
  69. MsgBox(ex.Message, MsgBoxStyle.Information)
  70.  
  71. End Try
  72.  
  73.  
  74.  
  75. End Sub
  76.  
  77. Public Sub jokenresult()
  78.  
  79. If list.Items.Count > 0 Then
  80.  
  81. list.Show()
  82.  
  83. myPlayer.Width = 577
  84.  
  85. statresult.Text = list.Items.Count & " Items"
  86.  
  87. Else
  88.  
  89. list.Hide()
  90.  
  91. myPlayer.Width = 787
  92.  
  93. End If
  94.  
  95.  
  96.  
  97. End Sub
  98.  
  99. Private Sub list_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles list.SelectedIndexChanged
  100.  
  101. 'the myPlayer will play or display something from the list based on the user selected item
  102.  
  103. myPlayer.URL = folderpath & "\" & list.SelectedItem.ToString
  104.  
  105. End Sub
  106.  
  107.  
  108.  
  109. Private Sub VideosToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VideosToolStripMenuItem.Click
  110.  
  111.  
  112.  
  113. Try
  114.  
  115. FolderBrowserDialog1.ShowDialog()
  116.  
  117. If DialogResult.OK Then
  118.  
  119.  
  120.  
  121. Dim di As New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
  122.  
  123. Dim diar1 As IO.FileInfo() = di.GetFiles("*.*")
  124.  
  125.  
  126.  
  127. Dim dra As IO.FileInfo
  128.  
  129. folderpath = di.FullName.ToString
  130.  
  131. list.Items.Clear()
  132.  
  133. For Each dra In diar1
  134.  
  135. list.Items.Add(dra)
  136.  
  137. Next
  138.  
  139. jokenresult()
  140.  
  141. End If
  142.  
  143. Catch ex As Exception
  144.  
  145. MsgBox(ex.Message, MsgBoxStyle.Information)
  146.  
  147. End Try
  148.  
  149.  
  150.  
  151. MsgBox(folderpath)
  152.  
  153.  
  154.  
  155. End Sub
  156.  
  157.  
  158.  
  159. Private Sub ImagesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImagesToolStripMenuItem.Click
  160.  
  161. Try
  162.  
  163. FolderBrowserDialog1.ShowDialog()
  164.  
  165. If DialogResult.OK Then
  166.  
  167.  
  168.  
  169. Dim di As New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
  170.  
  171. Dim diar1 As IO.FileInfo() = di.GetFiles("*.jpg")
  172.  
  173. Dim dra As IO.FileInfo
  174.  
  175. folderpath = di.FullName.ToString
  176.  
  177. list.Items.Clear()
  178.  
  179.  
  180.  
  181. For Each dra In diar1
  182.  
  183. list.Items.Add(dra)
  184.  
  185. Next
  186.  
  187. jokenresult()
  188.  
  189. End If
  190.  
  191. Catch ex As Exception
  192.  
  193. MsgBox(ex.Message, MsgBoxStyle.Information)
  194.  
  195. End Try
  196.  
  197. End Sub
  198.  
  199. Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
  200.  
  201. Me.Close()
  202.  
  203. End Sub
  204.  
  205. Private Sub PlaylistEditorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlaylistEditorToolStripMenuItem.Click
  206.  
  207. 'in this line if the playlist editor is click then the list will sho on the form.
  208.  
  209. If PlaylistEditorToolStripMenuItem.Checked = True Then
  210.  
  211. list.Show()
  212.  
  213. myPlayer.Width = 577
  214.  
  215. Else
  216.  
  217.  
  218.  
  219. list.Hide()
  220.  
  221. myPlayer.Width = 787
  222.  
  223. End If
  224.  
  225. End Sub
  226.  
  227.  
  228.  
  229. Private Sub mainFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  230.  
  231. list.Items.Clear()      ' clear all currect content of the list
  232.  
  233. list.Hide()             ' it will hide the on the main form
  234.  
  235. myPlayer.Width = 787    ' it will resize the width of myPlayer into 787
  236.  
  237. End Sub
  238.  
  239. End Class

You can download the source Code here.Mediaplayer

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!