• We just launched and are currently in beta. Join us as we build and grow the community.

Media Player control in Visual Basic.NET

HAHAA123

First Blood Expert
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
193
Likes
82
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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.

libraries_submenu.png

view_submenu.png


And the final design looks like as shown below.
main.png


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. If

    DialogResult.

    OK

    Then
  6. 'if true that if you click ok on the folder dialog box then
  7. 'it will get the selected path of your folder and store it into di variable
  8. Dim

    di As

    New

    IO.

    DirectoryInfo

    (

    FolderBrowserDialog1.

    SelectedPath

    )
  9. 'in this line of code it will get all the specific file that has the .mp3 extension and store it into diar1 variable
  10. Dim

    diar1 As

    IO.

    FileInfo

    (

    )

    =

    di.

    GetFiles

    (

    "*.mp3"

    )
  11. Dim

    dra As

    IO.

    FileInfo
  12. 'and in this line it will gather all information with regardsto fullpath and names of all files and store it to the folderpath variable
  13. folderpath =

    di.

    FullName

    .

    ToString
  14. list.

    Items

    .

    Clear

    (

    )
  15. ' list the names of all files in the specified directory

  16. For

    Each

    dra In

    diar1
  17. Dim

    a As

    Integer

    =

    0
  18. ' a = a + 1
  19. list.

    Items

    .

    Add

    (

    dra)
  20. Next
  21. 'it will call the sub procedure jokenresult() to perform some actions
  22. jokenresult(

    )
  23. End

    If
  24. Catch

    ex As

    Exception
  25. 'if errors occur then the program will catch it and send it back to the user.
  26. MsgBox

    (

    ex.

    Message

    , MsgBoxStyle.

    Information

    )
  27. End

    Try

  28. End

    Sub

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

playing_music.png


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

playing_video.png


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. Private

    Sub

    MusicToolStripMenuItem_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    MusicToolStripMenuItem.

    Click
  8. Try
  9. 'it will open the folder dialog where you can select where is the specific folder of your music
  10. FolderBrowserDialog1.

    ShowDialog

    (

    )

  11. If

    DialogResult.

    OK

    Then
  12. 'if true that if you click ok on the folder dialog box then
  13. 'it will get the selected path of your folder and store it into di variable
  14. Dim

    di As

    New

    IO.

    DirectoryInfo

    (

    FolderBrowserDialog1.

    SelectedPath

    )
  15. 'in this line of code it will get all the specific file that has the .mp3 extension and store it into diar1 variable
  16. Dim

    diar1 As

    IO.

    FileInfo

    (

    )

    =

    di.

    GetFiles

    (

    "*.mp3"

    )
  17. Dim

    dra As

    IO.

    FileInfo
  18. 'and in this line it will gather all information with regardsto fullpath and names of all files and store it to the folderpath variable
  19. folderpath =

    di.

    FullName

    .

    ToString
  20. list.

    Items

    .

    Clear

    (

    )
  21. ' list the names of all files in the specified directory

  22. For

    Each

    dra In

    diar1
  23. Dim

    a As

    Integer

    =

    0
  24. ' a = a + 1
  25. list.

    Items

    .

    Add

    (

    dra)
  26. Next
  27. 'it will call the sub procedure jokenresult() to perform some actions
  28. jokenresult(

    )
  29. End

    If
  30. Catch

    ex As

    Exception
  31. 'if errors occur then the program will catch it and send it back to the user.
  32. MsgBox

    (

    ex.

    Message

    , MsgBoxStyle.

    Information

    )
  33. End

    Try

  34. End

    Sub
  35. Public

    Sub

    jokenresult(

    )
  36. If

    list.

    Items

    .

    Count

    >

    0

    Then
  37. list.

    Show

    (

    )
  38. myPlayer.

    Width

    =

    577

  39. statresult.

    Text

    =

    list.

    Items

    .

    Count

    &

    " Items"
  40. Else
  41. list.

    Hide

    (

    )
  42. myPlayer.

    Width

    =

    787
  43. End

    If

  44. End

    Sub
  45. Private

    Sub

    list_SelectedIndexChanged(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    list.

    SelectedIndexChanged
  46. 'the myPlayer will play or display something from the list based on the user selected item
  47. myPlayer.

    URL

    =

    folderpath &

    "\"

    &

    list.

    SelectedItem

    .

    ToString
  48. End

    Sub

  49. Private

    Sub

    VideosToolStripMenuItem_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    VideosToolStripMenuItem.

    Click

  50. Try
  51. FolderBrowserDialog1.

    ShowDialog

    (

    )
  52. If

    DialogResult.

    OK

    Then

  53. Dim

    di As

    New

    IO.

    DirectoryInfo

    (

    FolderBrowserDialog1.

    SelectedPath

    )
  54. Dim

    diar1 As

    IO.

    FileInfo

    (

    )

    =

    di.

    GetFiles

    (

    "*.*"

    )

  55. Dim

    dra As

    IO.

    FileInfo
  56. folderpath =

    di.

    FullName

    .

    ToString
  57. list.

    Items

    .

    Clear

    (

    )
  58. For

    Each

    dra In

    diar1
  59. list.

    Items

    .

    Add

    (

    dra)
  60. Next
  61. jokenresult(

    )
  62. End

    If
  63. Catch

    ex As

    Exception
  64. MsgBox

    (

    ex.

    Message

    , MsgBoxStyle.

    Information

    )
  65. End

    Try

  66. MsgBox

    (

    folderpath)

  67. End

    Sub

  68. Private

    Sub

    ImagesToolStripMenuItem_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    ImagesToolStripMenuItem.

    Click
  69. Try
  70. FolderBrowserDialog1.

    ShowDialog

    (

    )
  71. If

    DialogResult.

    OK

    Then

  72. Dim

    di As

    New

    IO.

    DirectoryInfo

    (

    FolderBrowserDialog1.

    SelectedPath

    )
  73. Dim

    diar1 As

    IO.

    FileInfo

    (

    )

    =

    di.

    GetFiles

    (

    "*.jpg"

    )
  74. Dim

    dra As

    IO.

    FileInfo
  75. folderpath =

    di.

    FullName

    .

    ToString
  76. list.

    Items

    .

    Clear

    (

    )

  77. For

    Each

    dra In

    diar1
  78. list.

    Items

    .

    Add

    (

    dra)
  79. Next
  80. jokenresult(

    )
  81. End

    If
  82. Catch

    ex As

    Exception
  83. MsgBox

    (

    ex.

    Message

    , MsgBoxStyle.

    Information

    )
  84. End

    Try


  85. End

    Sub


  86. Private

    Sub

    ExitToolStripMenuItem_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    ExitToolStripMenuItem.

    Click
  87. Me

    .

    Close

    (

    )

  88. End

    Sub

  89. Private

    Sub

    PlaylistEditorToolStripMenuItem_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    PlaylistEditorToolStripMenuItem.

    Click
  90. 'in this line if the playlist editor is click then the list will sho on the form.
  91. If

    PlaylistEditorToolStripMenuItem.

    Checked

    =

    True

    Then
  92. list.

    Show

    (

    )
  93. myPlayer.

    Width

    =

    577
  94. Else

  95. list.

    Hide

    (

    )
  96. myPlayer.

    Width

    =

    787

  97. End

    If
  98. End

    Sub

  99. Private

    Sub

    mainFrm_Load(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    MyBase

    .

    Load
  100. list.

    Items

    .

    Clear

    (

    )

    ' clear all currect content of the list
  101. list.

    Hide

    (

    )

    ' it will hide the on the main form
  102. myPlayer.

    Width

    =

    787

    ' it will resize the width of myPlayer into 787
  103. End

    Sub
  104. End

    Class


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,340

323,349

Top