VB.NET - Mp3 Player - Asked By jenn on 20-Aug-10 07:12 PM

hey guys :)

Been working on this MP3 Player for a bit of time, trying to figure out how to get the listbox to shuffle or randomize play on a button click. Have looked high and low and have tried many ways, but to no avail! LOL..seems it is a deeply regarded secret! hehehehee

anyway, I am hoping someone can help me?

here is my code for the player so far:
Imports WMPLib
Public Class frmMain
  Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  End Sub
  Private Sub ofd_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ofd.FileOk
  End Sub
  
  
#Region "ListBoxes"
  Private Sub Listbox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Listbox1.SelectedIndexChanged
  
  End Sub
  Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
    Listbox1.SelectedIndex = ListBox2.SelectedIndex
  End Sub
#End Region
  
    
#Region "Timers"
  
  Private Sub TmrUpdateInfo_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TmrUpdateInfo.Tick
    Try
      If wmp.playState = WMPLib.WMPPlayState.wmppsPlaying Then
        lblTrackInfo.Text = "Now Playing:...." + wmp.currentMedia.name + Environment.NewLine + "Time:..." + wmp.Ctlcontrols.currentPositionString + "/" + wmp.currentMedia.durationString
      Else
        lblTrackInfo.Text = "Not Playing...."
      End If
    Catch ex As Exception
      MsgBox("An Error Occured!" + Environment.NewLine + ex.Message.ToString, MsgBoxStyle.Critical + MsgBoxStyle.MsgBoxSetForeground)
    End Try
  End Sub
  Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If CheckBox1.Checked = True Then
      If wmp.playState = WMPPlayState.wmppsPlaying = True Then
      ElseIf wmp.playState = WMPPlayState.wmppsStopped = True Then
        Listbox1.SelectedIndex = Listbox1.SelectedIndex + 1
        ListBox2.SelectedIndex = ListBox2.SelectedIndex + 1
        wmp.URL = Listbox1.SelectedItem
      ElseIf CheckBox1.Checked = False Then
        If wmp.playState = WMPPlayState.wmppsPlaying = False Then
          Listbox1.SelectedIndex = Listbox1.SelectedIndex - 1
          ListBox2.SelectedIndex = ListBox2.SelectedIndex - 1
          wmp.URL = Listbox1.SelectedItem
        End If
      End If
    End If
  End Sub
#End Region
  
  
#Region "Button Events"
  Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
    If Listbox1.Items.Count = 0 = True Then
      MsgBox("Nothing To Play", MsgBoxStyle.Critical, "Nothing To Play!")
    Else
      wmp.URL = Listbox1.SelectedItem
    End If
  End Sub
  
  Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
    wmp.Ctlcontrols.stop()
  End Sub
  
  Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click
    wmp.settings.volume = wmp.settings.volume + 10
  End Sub
  
  Private Sub btnDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDown.Click
    wmp.settings.volume = wmp.settings.volume - 10
  End Sub
  
  Private Sub btnMute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMute.Click
    If wmp.settings.volume = wmp.settings.mute() Then
      wmp.settings.volume = 10
    Else
      wmp.settings.volume = wmp.settings.mute()
    End If
  End Sub
  Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    Listbox1.SelectedIndex = Listbox1.SelectedIndex + 1
    ListBox2.SelectedIndex = ListBox2.SelectedIndex + 1
  End Sub
  
  Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
    Listbox1.SelectedIndex = Listbox1.SelectedIndex - 1
    ListBox2.SelectedIndex = ListBox2.SelectedIndex - 1
  End Sub
  
  Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
    Listbox1.Items.Remove(Listbox1.SelectedItem)
    ListBox2.Items.Remove(ListBox2.SelectedItem)
  End Sub
  
  Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    Listbox1.Items.Clear()
    ListBox2.Items.Clear()
  End Sub
  Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
    Dim ofd As New OpenFileDialog
    ofd.Multiselect = True
    ofd.ShowDialog()
    For I As Integer = 0 To ofd.FileNames.Count - 1
      Listbox1.Items.Add(ofd.FileNames(I))
      ListBox2.Items.Add(ofd.SafeFileNames(I))
    Next
    If Listbox1.Items.Count = 0 = True Then
      MsgBox("Nothing To Play!", MsgBoxStyle.Critical, "File Error!")
    Else
      Listbox1.SelectedIndex = 0
      ListBox2.SelectedIndex = 0
  
    End If
  End Sub
#End Region
  
End Class
I am also having trouble with a pause button. the standard pause code will pause but then upon trying to play from a paused states, makes the song start over again LOL..as you will notice I do not have a pause button in the code nor do I have a "shuffle" button in the code cuz i am to the point of ripping my hair out over these two issues LOL..that for now those 2 have been removed..

anyway, thank you for any help you can give me before I am completely bald!! heheheee
Rolf Jaeger replied to jenn on 21-Aug-10 12:00 AM
Hi Jenn:

I am not familiar with the wmp class you are utilizing, but after looking over your code I have a couple of comments and questions:

1) Does the statement wmp.URL = Listbox1.SelectedItem  actually cause the wmp object to play the selected MP3 file? If that doesn't work it COULD be that you might have to use 'ListBox1.SelectedItem.ToString()'. Or could it be that you need to use another method of the class (is there something like a wmp.Play ?)

2) It is not clear to me why you keep the no-action event handlers in your code. Since they don't do anything they should be removed to unclutter your code. I am referring to

frmMain_Load
ofd_FileOk
Listbox1_SelectedIndexChanged

3) The first statement in the btnNext_Click eventhandler is superfluous. The ListbBox2_SelectedIndexChanged event handler takes care of that. You could reduce the handler to:

  Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
  ListBox2.SelectedIndex = ListBox2.SelectedIndex + 1
  End Sub

4) In order for anyone to comment on your difficulties with continuing from a Pause state you should share the code you have tried so far

Hope this helped,
Rolf
jenn replied to Rolf Jaeger on 21-Aug-10 08:32 AM
good morning and thank you for the interest :)

yes, there are probably many other things I can do to clean up this code LOL..there seems to be remnants in there from all the "try this and see if this works" LOL..but to answer your first question about the wmp.URL=listbox1. yes, that is working just as it should. I am sure that there is probably another way to achieve this, lol..but this is the first time I have attempted to make a media player, and after much looking around, I have found that that particular statement seems to be the norm.

Everything is working as planned up to now. and I even finally found out about the pause and then resuming from the paused state, so that is good! :)

right now, I have 2 listboxes..one stacked on top of the other so that at run time..only listbox 2 is visible. the reason for this is so that if I go ...lets say into my D drive. I want to select 30 or 40 of my songs from my music folder...I simply highlight my songs and add them to the listbox. Now what happens is that in the HIDDEN listbox (listbox1) they are added in the form of "Paths" : like this....d:/Music/"name of song/...yadda yadda yadda (the long version) however, in listbox 2..the one that is visible shows as a short name: like this.."Bad Apple.Mp3".

I will for sure try the suggestion you are talking of for the "Next" button..but it is kinda scary as the way that this is set up that any changes that take place in "listbox2" automatically affect "Listbox1"..lol..but I am sure in this case, it would probably be a GOOD effect :)

so...

the pause/resume trouble has been taken care of by this:


Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click
    If (wmp.playState = WMPPlayState.wmppsPlaying) Then
      wmp.Ctlcontrols.pause()
    Else
      wmp.Ctlcontrols.play()
    End If
  End Sub

what I would REALLY like to know is how to get my "Playlist" (the listbox) items to randomly play on a click event. I have tried several "randomization" codings, but for some reason they just dont seem to do it for me.



Rolf Jaeger replied to jenn on 21-Aug-10 03:22 PM
Hi Jenn:

I am glad to hear you have been making progress with your media player project. Your strategy of how to get the media player to play random tunes seems sound to me. Here's what still seems to be missing from your code:

1) a statement like this:

{name of ListBox}.SelectedIndex = n

where 'ListBox' is the ListBox containing the path names of your media files, and n is the  n is a random number between 0 and the maximum number of items on your list (minus 1).

2) an event handler for the SelectedIndex_Changed event of that Listbox that includes this statement:

wmp.URL = {name of ListBox}.SelectedItem

Please let me know if that's not sufficiently clear.

Best wishes,
Rolf
jenn replied to Rolf Jaeger on 21-Aug-10 07:32 PM
okay, :)

i will sit down tonight and see if I cant make a go at it!
will let you know how it turns out in the morning LOL..boy o boy is this ever a task!

again thank you for takeing the time :)