Hi everyone, I am new on VB and recently I have a project in building a simple mp3 player. I come out with the design but I need some help with coding for some button and listbox....
my question is :
1. for [button 3] , I want to use it to search only folders (not file) in my computer, so the folder come out to the Listbox2. Then when I click the folder in the ListBox2, the files(songs name.mp3) will show up in the ListBox1. What is the code for this ?
2. How to make auto play NEXT song after previous song ended ?
I am sorry if I posted to the wrong thread,
Thank you very much
Visual Basic .NET questions would go into the top forum of the same name. No need to post a new thread however, a Moderator will move this one now that we know where it should go.
Visual Basic .NET questions would go into the top forum of the same name. No need to post a new thread however, a Moderator will move this one now that we know where it should go.
@jdc...did you report this to Moderators so they COULD move it?
I didn't know if jdc reported it or not, so I did report it as a backup in case it wasn't reported. It appears the moderators are busy elsewhere, as there is another thread with posts that need to be moved which haven't been moved yet. It is all volunteer though so real life sometimes imposes on our forum life.
The moderator didn't forget, the moderator didn't know. I moved the thread from the CodeBank, but it was in the VB6 CodeBank, so I moved it to the VB6 forum. Now that it is .NET, it needed to be reported again (which it was), to let us know that it was still in the wrong place.
Its in the right place, but your request is rather wide open and probably not too many people want to volunteer to spend their time narrowing down your requirements.
For instance if you hit Button 3, if you want it to search your whole disk looking for folders that contains mp3 files, that is a fairly long operation. Just open Windows explorer, and go to your root drive, and search for *.mp3 files and watch how long it takes.
I don't really feel like taking the time to work on an approach to search and gather the files you want and perhaps others don't want to either.
As far as knowing when one song has ended, and starting another, you can watch for the PlayStateChange event and look for PlayState = 8 'MediaEnded . {don't know if there is a named constant for 8, msdn example code has hardcoded literals}
You can then schedule the next song to be played. You can't actually start it from the event handler as that causes the media player to hang (I assume because you cause an event from an event handler which it doesn't handle correctly).
I use the BeginInvoke method to cause the sub that starts the next song to execute after the current event handler sub is exited, so there is no conflict.
I'll post a fairly basic test I did of using the Windows Media Player from VB.Net. It is just a start, to test using the player, not a fully functional application.
It will play audio files, and show a handful of different video file types as well.
Rather than add logic to the application that searches for files to play, I just accept files being dropped on the application.
You run the program and open Windows Explorer yourself and find files that you want to play. You can select multiple files (not directories, but perhaps that is something you could add), and drag and drop them on the player.
The player loops through all the files dropped on it and pushes them onto a queue.
If nothing is currently playing, it calls a dequeue sub, which pops one off the queue and plays it.
When a song ends, another call to the dequeue sub pops off the next song and plays it.
This continues until the queue is empty.
You can drag more files and drop them on the player while it is playing songs from the queue. New files dropped just get added to the queue, it doesn't interrupt or affect songs already in the queue.
If I was doing more than a simple test, I would be maintaining a list myself, and providing my own management of the list or lists, and providing list manipulation features, etc..., but this was just a quick test and I have no real interest in creating a custom media player at the moment.
It has only three buttons. If you hit stop, it stops playing the current song and moves on to the next from the queue.
If you hit pause, it pauses the song. You have to hit play to continue after pausing.
I only added a label to control volume at the end because someone wanted an example of controlling the volume.
I'll post this vs2010 version of the test player, and you can learn what you can from it, if anything.
p.s. Forgot to mention you can drag on the status label to change the play position.
You also drag on the other label to change the volume.
In case someone is interested in looking at the code without downloading and unzipping, I'll post that as well.
Code:
Imports System.IO
Public Class Form1
Dim songQueue As New Queue(Of String)
Dim playing As Boolean
Dim ProgressBrush As SolidBrush = New SolidBrush(Color.FromArgb(32, 0, 0, 255))
Dim duration As Double, max As Double
Dim place As Single
Dim updatePlace As Boolean
Dim videoExts As String = " .mp4 .mpg .mpeg .wmv .avi" 'List of some video type file extensions
Private Sub Form1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
Dim s() As String
s = e.Data.GetData(DataFormats.FileDrop)
For i As Integer = 0 To UBound(s)
songQueue.Enqueue(s(i))
Next
If Not playing Then
playing = True
DeQueue_a_song()
End If
End Sub
Private Sub DeQueue_a_song()
If songQueue.Count > 0 Then
AxWindowsMediaPlayer1.URL = songQueue.Dequeue()
Dim ext As String = Path.GetExtension(AxWindowsMediaPlayer1.URL) 'Get the file's extension
ext = ext.ToLower() 'convert it to lower case
If videoExts.IndexOf(ext, 0) > 0 Then 'if it is in the list of video file extensions Then
AxWindowsMediaPlayer1.uiMode = "None" ' Make sure the wmp gui is not shown
AxWindowsMediaPlayer1.stretchToFit = True ' Stretch the video to fit the window (window will resize with form)
AxWindowsMediaPlayer1.Visible = True ' make the wmp window visible
Else 'Else (assume it is an audio file
AxWindowsMediaPlayer1.Visible = False ' hide the video window
End If
Else
Me.Text = "Queue Empty"
playing = False
End If
End Sub
Private Sub Form1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
e.Effect = DragDropEffects.All 'won't get drop event if you don't set e.Effect to something
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 200
Timer1.Start()
End Sub
Private Sub btnStop_Click(sender As System.Object, e As System.EventArgs) Handles btnStop.Click
' AxWindowsMediaPlayer1.Ctlcontrols.stop()
DeQueue_a_song() 'just move on to the next song if Stop clicked
End Sub
Private Sub btnPause_Click(sender As System.Object, e As System.EventArgs) Handles btnPause.Click
AxWindowsMediaPlayer1.Ctlcontrols.pause()
End Sub
Private Sub btnPlay_Click(sender As System.Object, e As System.EventArgs) Handles btnPlay.Click
AxWindowsMediaPlayer1.Ctlcontrols.play()
playing = True
End Sub
Private Sub AxWindowsMediaPlayer1_MediaError(sender As Object, e As AxWMPLib._WMPOCXEvents_MediaErrorEvent) Handles AxWindowsMediaPlayer1.MediaError
Try
' If the file is corrupt or missing, show the
' hexadecimal error code and URL.
Dim errSource As WMPLib.IWMPMedia2 = e.pMediaObject
Dim errorItem As WMPLib.IWMPErrorItem = errSource.Error
MessageBox.Show("Media error " + errorItem.errorCode.ToString("X") _
+ " in " + errSource.sourceURL)
Catch ex As InvalidCastException
' In case pMediaObject is not an IWMPMedia item.
MessageBox.Show("Player error.")
Finally 'move on in any case
Me.BeginInvoke(Sub() DeQueue_a_song()) 'file had an error, move on to the next one
End Try
End Sub
Private Sub AxWindowsMediaPlayer1_PlayStateChange(sender As Object, e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange
Select Case e.newState
Case 8 'Media Ended
'We want to start another song, but can't do it from within a media player event handler,
' as it may cause it to hang (cause an event from within an event).
' We use BeginInvoke, which will call the sub later, after we've exited this event.
Me.BeginInvoke(Sub() DeQueue_a_song()) 'file had an error, schedule a try to move to next
End Select
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If playing Then
If updatePlace Then
updatePlace = False
AxWindowsMediaPlayer1.Ctlcontrols.currentPosition = place
End If
Label1.Invalidate() 'Update the playing song's status
End If
End Sub
Private Sub Label1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
Dim pos As Single
pos = e.X
max = Label1.ClientSize.Width
If pos > max Then pos = max
If pos < 0 Then pos = 0
Dim ds As String = "00:" & AxWindowsMediaPlayer1.currentMedia.durationString
duration = TimeSpan.Parse(ds).TotalSeconds
place = duration * (pos / max)
updatePlace = True
End Sub
Private Sub Label1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim pos As Single
pos = e.X
max = Label1.ClientSize.Width
If pos > max Then pos = max
If pos < 0 Then pos = 0
Dim ds As String = "00:" & AxWindowsMediaPlayer1.currentMedia.durationString
duration = TimeSpan.Parse(ds).TotalSeconds
place = duration * (pos / max)
updatePlace = True
End If
End Sub
Private Sub Label1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
Dim pos As Double = AxWindowsMediaPlayer1.Ctlcontrols.currentPosition
Dim dss As String
max = Label1.ClientSize.Width
If AxWindowsMediaPlayer1.currentMedia IsNot Nothing Then
Dim ds As String = "00:" & AxWindowsMediaPlayer1.currentMedia.durationString
duration = TimeSpan.Parse(ds).TotalSeconds
place = 0
If duration >= 1 Then
place = pos * (max / duration)
End If
e.Graphics.FillRectangle(ProgressBrush, 0, 0, place, Label1.ClientSize.Height)
dss = String.Format("{0}, {1} {2}", AxWindowsMediaPlayer1.currentMedia.name, _
AxWindowsMediaPlayer1.Ctlcontrols.currentPositionString, AxWindowsMediaPlayer1.currentMedia.durationString)
Dim p As Single = max - e.Graphics.MeasureString(dss, Me.Font).Width
e.Graphics.DrawString(dss, Me.Font, Brushes.Black, p, 0)
End If
End Sub
Private Sub Label2_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Label2.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then ProcessVolumeInput(sender, e)
End Sub
Private Sub Label2_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Label2.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then ProcessVolumeInput(sender, e)
End Sub
Private Sub ProcessVolumeInput(sender As System.Object, e As System.Windows.Forms.MouseEventArgs)
Dim lbl As Label = DirectCast(sender, Label)
Dim vol As Integer = 100 * (e.X / lbl.ClientSize.Width) 'convert to value relative to label width representing 100%
vol = Math.Max(Math.Min(100, vol), 0) 'limit to 0 to 100 range
AxWindowsMediaPlayer1.settings.volume = vol
Label2.Invalidate()
End Sub
Private Sub Label2_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Label2.Paint
Dim vol As Integer = AxWindowsMediaPlayer1.settings.volume
Dim ds As String = vol.ToString 'the volume as a string
e.Graphics.FillRectangle(ProgressBrush, 0, 0, CInt((vol / 100) * Label2.ClientSize.Width), Label2.ClientSize.Height)
Dim p As Single = (Label2.ClientSize.Width - e.Graphics.MeasureString(ds, Me.Font).Width) / 2 'center string
e.Graphics.DrawString(ds, Me.Font, Brushes.Black, p, 0) 'draw volume text centered
End Sub
End Class
hi passel, Thanks alot. I appreciate your help in giving me the idea how is my project supposed to be. but I still try hard to realize my simple idea. I have this simple design of mp3 player but I don't know the correct code. I've tried learn it from youtube, but still does not work correctly. I have 2 listbox in my mp3 design, the bottom one is for folder seach only ( I use folderBrowserDialog ) and the top right listbox is for playlist which is for mp3 file ( songs name only ). So, once I click Load Album button, it will search for folder then I click the folder, all files in the folder will come out in the bottom listbox and at the same time "songs.mp3" files come up in the upper right listbox. But I don't know the correct code for them. Could anyone help me please....thanks alot
This is exactly what I want my mp3 player looks like....( just a sample )
Last edited by fortunatekidz; Mar 1st, 2018 at 10:52 PM.