Hi all,
Using VB.Net and WMP 10
Can enyone help me to make the form1 play a random song from a "songs" folder in the same directory?
I managed to list the contents "*.MP3" files in a listBox in the form. but I couldn't figure how to configure this list as a mediaplayer list!!
Is it possible to make it play randomly from that list?
below is the code I'm using.
Thank you in Advance.
+++++++++++++++++
Code begin
+++++++++++++++++
Imports AxWMPLib
Imports WMPLib
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Dim foldDial = New System.Windows.Forms.Folde
rBrowserDi
alog
Private SongFolder As System.String
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon
tainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btn_Exit As System.Windows.Forms.Butto
n
Friend WithEvents lbl_path As System.Windows.Forms.Label
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenF
ileDialog
Friend WithEvents ListBox1 As System.Windows.Forms.ListB
ox
Friend WithEvents AxWindowsMediaPlayer1 As AxWMPLib.AxWindowsMediaPla
yer
<System.Diagnostics.Debugg
erStepThro
ugh()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceM
anager = New System.Resources.ResourceM
anager(Get
Type(Form1
))
Me.btn_Exit = New System.Windows.Forms.Butto
n
Me.lbl_path = New System.Windows.Forms.Label
Me.OpenFileDialog1 = New System.Windows.Forms.OpenF
ileDialog
Me.ListBox1 = New System.Windows.Forms.ListB
ox
Me.AxWindowsMediaPlayer1 = New AxWMPLib.AxWindowsMediaPla
yer
CType(Me.AxWindowsMediaPla
yer1, System.ComponentModel.ISup
portInitia
lize).Begi
nInit()
Me.SuspendLayout()
'
'btn_Exit
'
Me.btn_Exit.FlatStyle = System.Windows.Forms.FlatS
tyle.Syste
m
Me.btn_Exit.Location = New System.Drawing.Point(402, 237)
Me.btn_Exit.Name = "btn_Exit"
Me.btn_Exit.Size = New System.Drawing.Size(105, 24)
Me.btn_Exit.TabIndex = 1
Me.btn_Exit.Text = "&Exit"
'
'lbl_path
'
Me.lbl_path.FlatStyle = System.Windows.Forms.FlatS
tyle.Syste
m
Me.lbl_path.Location = New System.Drawing.Point(24, 159)
Me.lbl_path.Name = "lbl_path"
Me.lbl_path.Size = New System.Drawing.Size(210, 23)
Me.lbl_path.TabIndex = 3
Me.lbl_path.Text = "Label1"
'
'ListBox1
'
Me.ListBox1.Location = New System.Drawing.Point(12, 24)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(207, 95)
Me.ListBox1.TabIndex = 4
'
'AxWindowsMediaPlayer1
'
Me.AxWindowsMediaPlayer1.E
nabled = True
Me.AxWindowsMediaPlayer1.L
ocation = New System.Drawing.Point(357, 159)
Me.AxWindowsMediaPlayer1.N
ame = "AxWindowsMediaPlayer1"
Me.AxWindowsMediaPlayer1.O
cxState = CType(resources.GetObject(
"AxWindows
MediaPlaye
r1.OcxStat
e"), System.Windows.Forms.AxHos
t.State)
Me.AxWindowsMediaPlayer1.T
abIndex = 5
Me.AxWindowsMediaPlayer1.V
isible = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(550, 296)
Me.Controls.Add(Me.AxWindo
wsMediaPla
yer1)
Me.Controls.Add(Me.ListBox
1)
Me.Controls.Add(Me.lbl_pat
h)
Me.Controls.Add(Me.btn_Exi
t)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.AxWindowsMediaPla
yer1, System.ComponentModel.ISup
portInitia
lize).EndI
nit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btn_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Exit.Click
Me.Close()
End Sub
Private Sub FillSongsListBox(ByVal path As String)
If path.Length = 0 Then
Return
End If
Dim di As DirectoryInfo = New DirectoryInfo(path)
Dim fi() As FileInfo = di.GetFiles("*.mp3")
Me.ListBox1.Items.Clear()
Me.ListBox1.Text = ""
Dim nxFi As FileInfo
For Each nxFi In fi
Me.ListBox1.Items.Add(nxFi
.Name)
Next
End Sub
Private Function LoadPathFromRegisTry() As String
'RegistryKey rk = Registry.LocalMachine.Open
SubKey("SO
FTWARE\Ski
nCrafter\S
kinCrafter
Demo");
'return rk.GetValue("SkinPath").To
String();
Return ""
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'load songs Folder
foldDial.SelectedPath = "songs"
SongFolder = foldDial.SelectedPath
Me.lbl_path.Text = SongFolder
FillSongsListBox(SongFolde
r)
'-------------------------
---
End Sub
End Class
+++++++++++++++++
Code End
+++++++++++++++++