i just want to make a mp3 player, but i got problem at my mp3 path. this is my code:

Public Class Form1

    Private Sub Import_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Import.Click
        FBD.ShowDialog()
        TextBox1.Text = FBD.SelectedPath
        Dim folderInfo As New IO.DirectoryInfo(TextBox1.Text)
        Dim arrFilesInFolder() As IO.FileInfo
        Dim fileInFolder As IO.FileInfo
        arrFilesInFolder = folderInfo.GetFiles("*.mp3")
        For Each fileInFolder In arrFilesInFolder
            playlist.Items.Add(fileInFolder.Name)
        Next
    End Sub

    Private Sub Play_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Play.Click
        AxWindowsMediaPlayer1.URL = playlist.SelectedItem
    End Sub

my problem is on line AxWindowsMediaPlayer1.URL = playlist.SelectedItem because if you can see, my folder directory is in the textbox, but my mp3 name is in the playlis(listbox), so now i dont know how to combine this url.

Recommended Answers

All 14 Replies

Try :
playlist.Items.Add(TextBox1.Text & "\" & fileInFolder.Name)

Sorry, I mean this line :
AxWindowsMediaPlayer1.URL = TextBox1.Text & "\" & ListBox1.SelectedItem

commented: Nice!! Thanks! +1

can you help me how can i auto play next song from the listbox?

this is my latest code.

Public Class Form1

    Private Sub Import_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Import.Click
        FBD.ShowDialog()
        If FBD.SelectedPath = "" Then
            'do nothing
        Else
            TextBox1.Text = FBD.SelectedPath
            Dim folderInfo As New IO.DirectoryInfo(TextBox1.Text)
            Dim arrFilesInFolder() As IO.FileInfo
            Dim fileInFolder As IO.FileInfo
            arrFilesInFolder = folderInfo.GetFiles("*.mp3")
            For Each fileInFolder In arrFilesInFolder
                playlist.Items.Add(fileInFolder.Name)
            Next
        End If
    End Sub

    Private Sub Play_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Play.Click
        AxWindowsMediaPlayer1.URL = TextBox1.Text & "\" & playlist.SelectedItem
    End Sub

    Private Sub stopbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stopbutton.Click
        AxWindowsMediaPlayer1.Ctlcontrols.stop()
    End Sub

    Private Sub pause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pause.Click
        AxWindowsMediaPlayer1.Ctlcontrols.pause()
    End Sub

    Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As System.Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange
        If sender.playState = WMPLib.WMPPlayState.wmppsMediaEnded Then
            'put whatever you want here when current play ended
            playlist.SelectedIndices.Add(1)
            AxWindowsMediaPlayer1.URL = TextBox1.Text & "\" & playlist.SelectedItem
        End If
    End Sub

To play next song :

Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange
    If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsStopped Then
        ListBox1.SetSelected(ListBox1.SelectedIndex + 1, True)
        AxWindowsMediaPlayer1.URL = TextBox1.Text & "\" & ListBox1.SelectedItem
    End If
End Sub
commented: Thanks! +0

Jx Man,

can you explain to me about this line? ListBox1.SetSelected(ListBox1.SelectedIndex + 1, True)

the function of 'True'?

ListBox1.SetSelected(ListBox1.SelectedIndex + 1, True)
This line will set selected item on listbox with the index of item.
I use Listbox.SelectedIndex + 1 as a new index.
The current index of playing song is selected item, to get index of next item we need to add current index with 1.
Set as True to make the item with an index is selected.

More info about Listbox.SetSelected(index as Integer,value as Boolean)

Also you can use another approach to play next song.
You don't have to use PlayStateChange event of Windows Media Player but you can use timer control. Checking if song is ended and play next song.
Just add timer to form, set Enabled = True and Interval to 1.

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsStopped Then
        ListBox1.SetSelected(ListBox1.SelectedIndex + 1, True)
        AxWindowsMediaPlayer1.URL = TextBox1.Text & "\" & ListBox1.SelectedItem
    End If
End Sub

Hope it helps you to solve your problem.

thanks Jx man, but i thinks i still got problem here, i try to use your auto play next song and put at PlayStateChange it cant play next song, but i edit your code like this.

    Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As System.Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange
        If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsMediaEnded Then
            playlist.SetSelected(playlist.SelectedIndex + 1, True)
            AxWindowsMediaPlayer1.URL = TextBox1.Text & "\" & playlist.SelectedItem
        End If
        Me.AxWindowsMediaPlayer1.Ctlcontrols.play()
    End Sub

after that it can play next song, the problem now is for my stop button, after i click stop button the song will stop but it restart the song again. I also try put at timer, the problem is different, listbox not select the next line, it just playback the same song.

I have tried all posted code and works fine to me.
If you want to use timer then don't use playedstate event.
Also your if-then condition is different with my code above.
Read it carefully.
I prefer to use timer control to play next song.

ok, i already try copy and paste your code, and didnt use playstate and only use timer, and the player can play next song, but my problem now, my stop button, do you try use my stop button?
this is my stop button code,

    Private Sub stopbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stopbutton.Click
        AxWindowsMediaPlayer1.Ctlcontrols.stop()
    End Sub

Okay.. We need boolean variable here to handle what section now.

' Declare flag as global variable
Dim flag As Boolean = True

...


Private Sub PlaySong()
    AxWindowsMediaPlayer1.URL = TextBox1.Text & "\" & ListBox1.SelectedItem
    flag = True
End Sub

Private Sub NextSong()
   If flag = True Then
        If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsStopped Then
            ListBox1.SetSelected(ListBox1.SelectedIndex + 1, True)
            AxWindowsMediaPlayer1.URL = TextBox1.Text & "\" & ListBox1.SelectedItem
        End If
    End If
End Sub

Private Sub PlayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayButton.Click
    PlaySong()
End Sub

Private Sub StopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopButton.Click
    flag = False
    AxWindowsMediaPlayer1.Ctlcontrols.stop()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    NextSong()
End Sub

OK, Thanks Jv Man,
do you know how to make pause button? i already try to make it after i click play button the song will not continue but it will be restart. this is my code for pause button.

    Private Sub pause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pause.Click
        AxWindowsMediaPlayer1.Ctlcontrols.pause()
    End Sub

do you know how to make pause button? i already try to make it after i click play button the song will not continue but it will be restart. this is my code for pause button.

You need to check what is the wmp state. Simple logic here. If state is pause then play the current song else play the selected song.
This is the modification of PlaySong Procedure :

Private Sub PlaySong()
    If ListBox1.Items.Count > 0 Then
        If Player.playState = WMPLib.WMPPlayState.wmppsPaused Then
            Player.Ctlcontrols.play()
        Else
            Player.URL = TextBox1.Text & "\" & ListBox1.SelectedItem
            flag = True
        End If
    End If
End Sub

the same logic applies to the pause button. If state is pause then play the current song else pause the song.
Here is the code:

 Private Sub Pause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pause.Click
    If Player.playState = WMPLib.WMPPlayState.wmppsPaused Then
        Player.Ctlcontrols.play()
    Else
        Player.Ctlcontrols.pause()
    End If
End Sub

Hope it solve your problem

commented: Lots answer huh.. Why don't you make repeat or shufle function too :) +4

Thanks Jx Man!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.