You are on page 1of 12

25/10/2018 Trackbar Music Control

Search Visual Studio with Bing SIGN IN

GALLERY MSDN LIBRARY FORUMS


get started for free
Ask a question Search related threads Search forum questions

Quick access

Answered by: Trackbar Music Control


Visual Studio Languages , .NET Framework > Visual Basic
77,215
Points Question
Top 0.1%

Hi.
Mr. Monkeyboy
I'm looking for a way to make a trackbar on my form control music that plays through my
Joined May 2012
application. The trackbar would allow me to skip to different parts of the song.
Mr. Monkeyboy's …

Show activity
0 Also, how could I display the time left in the song and the total time that the song plays for in
4 5 17
Sign in labels?
to vote
Thx.

Bgeo99

Saturday, November 16, 2013 4:47 AM

Reply | Quote
Bgeo25 35 Points

Answers

1
Sign in
to vote

This seems to work pretty good. The system thread sleep is necessary to keep the WMP
control from running while the trackbar is being moved or else the track being played
attempts to perform all of the pause and plays for each increment of the track bar which is
ugly.

Option Strict On

Public Class Form1

Dim Media As New List(Of String)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase


Media.Add("C:\Users\John\Desktop\Apollo 16.wmv")
Media.Add("C:\Users\John\Desktop\Pegasus.wmv")
Media.Add("C:\Users\John\Music\Miami Vice Theme.mp3")
Media.Add("C:\Users\John\Desktop\teddybearstereo.wav")
Media.Add("C:\Users\John\Music\Phil_Collins_I_Dont_Care_Anymore.mp3
Media.Add("C:\Users\John\Music\freeway_jam.mp3")
Media.Add("C:\Users\John\Music\ZZ_Top__Viva_Las_Vegas.mp3")
For Each Item In Media
Dim SomeMedia As WMPLib.IWMPMedia = AxWindowsMediaPlayer1.newMe
AxWindowsMediaPlayer1.currentPlaylist.appendItem(SomeMedia)
Next
AxWindowsMediaPlayer1.settings.setMode("shuffle", True)

https://social.msdn.microsoft.com/Forums/vstudio/en-US/34ac95f5-4cad-4b11-937a-de2ee5896c04/trackbar-music-control?forum=vbgeneral 1/12
25/10/2018 Trackbar Music Control
TrackBar1.Minimum = 0
Timer1.Interval = 100
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles But


AxWindowsMediaPlayer1.Ctlcontrols.play()
Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer


TrackBar1.Maximum = CInt(AxWindowsMediaPlayer1.currentMedia.duratio
Label1.Text = AxWindowsMediaPlayer1.currentMedia.durationString
Label2.Text = AxWindowsMediaPlayer1.Ctlcontrols.currentPosition.ToS
TrackBar1.Value = CInt(AxWindowsMediaPlayer1.Ctlcontrols.currentPos
End Sub

Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles


AxWindowsMediaPlayer1.Ctlcontrols.pause()
AxWindowsMediaPlayer1.Ctlcontrols.currentPosition = CDbl(TrackBar1.V
System.Threading.Thread.Sleep(300)
AxWindowsMediaPlayer1.Ctlcontrols.play()
End Sub
End Class

https://social.msdn.microsoft.com/Forums/vstudio/en-US/34ac95f5-4cad-4b11-937a-de2ee5896c04/trackbar-music-control?forum=vbgeneral 2/12
25/10/2018 Trackbar Music Control

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of
DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you
post an image it clutters up the thread and mysteriously, over time, the link to the image will
somehow become "unstable" or something to that effect. :) I can only surmise that is due to
Global Warming of the threads.

Marked as answer by Bgeo25 Saturday, November 16, 2013 11:49 PM

Saturday, November 16, 2013 8:23 AM

Reply | Quote
Mr. Monkeyboy Nothing in particular 77,215 Points

All replies

https://social.msdn.microsoft.com/Forums/vstudio/en-US/34ac95f5-4cad-4b11-937a-de2ee5896c04/trackbar-music-control?forum=vbgeneral 3/12
25/10/2018 Trackbar Music Control

0
Sign in
to vote

Hi.

I'm looking for a way to make a trackbar on my form control music that plays through my
application. The trackbar would allow me to skip to different parts of the song.

Also, how could I display the time left in the song and the total time that the song plays
for in labels?

Thx.

Bgeo99

You make no mention of what control you are using to play the music.

If it is a windows media player control then you can use appropriate methods to display total
time and use a timer I suppose to get time left unless there is an event handler for that which I
am unaware of.

And in the track bar set its max value to whatever the current songs total time is I guess and
use appropriate math to convert the trackbars scale to move the location in the song, via call
to windows media player, to a new location I would imagine since that would be based on
time rather than decimal at that point.

If you are using Windows Media Player then you can look through this link at all the various
capabilities until you find the one that provides methods for getting information from the
windows media player for current songs duration and current songs "marker" location and
setting the "marker" location I believe is the term for the location in the song that you want it
to move to.

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of
DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you
post an image it clutters up the thread and mysteriously, over time, the link to the image will
somehow become "unstable" or something to that effect. :) I can only surmise that is due to
Global Warming of the threads.

Saturday, November 16, 2013 6:54 AM

Reply | Quote
Mr. Monkeyboy Nothing in particular 77,215 Points

0
Sign in
to vote

My bad. If you're using WMP control then you can use double to set the current position
although in the timer I am getting rather than setting the current position.

To get the actual time length use "Label1.Text =


AxWindowsMediaPlayer1.currentMedia.durationString" instead of duration.ToString and it will
show minutes/secs or I suppose hours/mins/secs of the track being played.

Option Strict On

Public Class Form1

https://social.msdn.microsoft.com/Forums/vstudio/en-US/34ac95f5-4cad-4b11-937a-de2ee5896c04/trackbar-music-control?forum=vbgeneral 4/12
25/10/2018 Trackbar Music Control

Dim Media As New List(Of String)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase


Media.Add("C:\Users\John\Desktop\Apollo 16.wmv")
Media.Add("C:\Users\John\Desktop\Pegasus.wmv")
Media.Add("C:\Users\John\Music\Miami Vice Theme.mp3")
Media.Add("C:\Users\John\Desktop\teddybearstereo.wav")
Media.Add("C:\Users\John\Music\Phil_Collins_I_Dont_Care_Anymore.mp3
Media.Add("C:\Users\John\Music\freeway_jam.mp3")
Media.Add("C:\Users\John\Music\ZZ_Top__Viva_Las_Vegas.mp3")
For Each Item In Media
Dim SomeMedia As WMPLib.IWMPMedia = AxWindowsMediaPlayer1.newMe
AxWindowsMediaPlayer1.currentPlaylist.appendItem(SomeMedia)
Next
AxWindowsMediaPlayer1.settings.setMode("shuffle", True)
Timer1.Interval = 100
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles But


AxWindowsMediaPlayer1.Ctlcontrols.play()
Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer


Label1.Text = AxWindowsMediaPlayer1.currentMedia.duration.ToString
Label2.Text = AxWindowsMediaPlayer1.Ctlcontrols.currentPosition.ToS
End Sub
End Class

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of
DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you
post an image it clutters up the thread and mysteriously, over time, the link to the image will
somehow become "unstable" or something to that effect. :) I can only surmise that is due to
Global Warming of the threads.

Edited by Mr. Monkeyboy Saturday, November 16, 2013 7:15 AM 5555

Saturday, November 16, 2013 7:12 AM

Reply | Quote
Mr. Monkeyboy Nothing in particular 77,215 Points

https://social.msdn.microsoft.com/Forums/vstudio/en-US/34ac95f5-4cad-4b11-937a-de2ee5896c04/trackbar-music-control?forum=vbgeneral 5/12
25/10/2018 Trackbar Music Control

1
Sign in
to vote

This seems to work pretty good. The system thread sleep is necessary to keep the WMP
control from running while the trackbar is being moved or else the track being played
attempts to perform all of the pause and plays for each increment of the track bar which is
ugly.

Option Strict On

Public Class Form1

Dim Media As New List(Of String)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase


Media.Add("C:\Users\John\Desktop\Apollo 16.wmv")
Media.Add("C:\Users\John\Desktop\Pegasus.wmv")
Media.Add("C:\Users\John\Music\Miami Vice Theme.mp3")
Media.Add("C:\Users\John\Desktop\teddybearstereo.wav")
Media.Add("C:\Users\John\Music\Phil_Collins_I_Dont_Care_Anymore.mp3
Media.Add("C:\Users\John\Music\freeway_jam.mp3")
Media.Add("C:\Users\John\Music\ZZ_Top__Viva_Las_Vegas.mp3")
For Each Item In Media
Dim SomeMedia As WMPLib.IWMPMedia = AxWindowsMediaPlayer1.newMe
AxWindowsMediaPlayer1.currentPlaylist.appendItem(SomeMedia)
Next
AxWindowsMediaPlayer1.settings.setMode("shuffle", True)
TrackBar1.Minimum = 0
Timer1.Interval = 100
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles But


AxWindowsMediaPlayer1.Ctlcontrols.play()
Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer


TrackBar1.Maximum = CInt(AxWindowsMediaPlayer1.currentMedia.duratio
Label1.Text = AxWindowsMediaPlayer1.currentMedia.durationString
Label2.Text = AxWindowsMediaPlayer1.Ctlcontrols.currentPosition.ToS
TrackBar1.Value = CInt(AxWindowsMediaPlayer1.Ctlcontrols.currentPos
End Sub

Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles


AxWindowsMediaPlayer1.Ctlcontrols.pause()
AxWindowsMediaPlayer1.Ctlcontrols.currentPosition = CDbl(TrackBar1.V
System.Threading.Thread.Sleep(300)
AxWindowsMediaPlayer1.Ctlcontrols.play()
End Sub
End Class

https://social.msdn.microsoft.com/Forums/vstudio/en-US/34ac95f5-4cad-4b11-937a-de2ee5896c04/trackbar-music-control?forum=vbgeneral 6/12
25/10/2018 Trackbar Music Control

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of
DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you
post an image it clutters up the thread and mysteriously, over time, the link to the image will
somehow become "unstable" or something to that effect. :) I can only surmise that is due to
Global Warming of the threads.

Marked as answer by Bgeo25 Saturday, November 16, 2013 11:49 PM

Saturday, November 16, 2013 8:23 AM

Reply | Quote
Mr. Monkeyboy Nothing in particular 77,215 Points

https://social.msdn.microsoft.com/Forums/vstudio/en-US/34ac95f5-4cad-4b11-937a-de2ee5896c04/trackbar-music-control?forum=vbgeneral 7/12
25/10/2018 Trackbar Music Control

0
Sign in
to vote

I used the code from above, but it still isn't working.. Maybe its because the trackbar
increment is incorrect? What should it be set to? I used the code below and the build of the
application was successful, but the media still won't play. Also, the trackbar freezes when I
press the button to play (Button1)..

Option Strict On
Public Class Form1

Dim Media As New List(Of String)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase


Media.Add("C:\Users\Bobby\My Received Files\Ahora.mp3")
For Each Item In Media
Dim SomeMedia As WMPLib.IWMPMedia = AxWindowsMediaPlayer1.newMe
AxWindowsMediaPlayer1.currentPlaylist.appendItem(SomeMedia)
Next
AxWindowsMediaPlayer1.settings.setMode("shuffle", True)
TrackBar1.Minimum = 0
Timer1.Interval = 100
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles But


AxWindowsMediaPlayer1.Ctlcontrols.play()
Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer


TrackBar1.Maximum = CInt(AxWindowsMediaPlayer1.currentMedia.duratio
Label1.Text = AxWindowsMediaPlayer1.currentMedia.durationString
Label2.Text = AxWindowsMediaPlayer1.Ctlcontrols.currentPosition.ToS
TrackBar1.Value = CInt(AxWindowsMediaPlayer1.Ctlcontrols.currentPos
End Sub

Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles


AxWindowsMediaPlayer1.Ctlcontrols.pause()
AxWindowsMediaPlayer1.Ctlcontrols.currentPosition = CDbl(TrackBar1.V
System.Threading.Thread.Sleep(300)
AxWindowsMediaPlayer1.Ctlcontrols.play()
End Sub
End Class

Bgeo99

Edited by Bgeo25 Saturday, November 16, 2013 4:33 PM

Saturday, November 16, 2013 4:27 PM

Reply | Quote
Bgeo25 35 Points

https://social.msdn.microsoft.com/Forums/vstudio/en-US/34ac95f5-4cad-4b11-937a-de2ee5896c04/trackbar-music-control?forum=vbgeneral 8/12
25/10/2018 Trackbar Music Control

0
Sign in
to vote

Well your code looks O.K.

I'm using Visual Studio 2012 and used the Windows Media Player control that is in my
toolbox.

Perhaps you should try using clean, then rebuild instead of build.

Half the time I build an app with the WMP in it the form disappears from the designer and for
that window a blank white page is displayed with an error. Then I have to close and reopen
the project for some reason.

Although I just noticed I have a reference to WMP.Dll set in browse for references.

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of
DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you
post an image it clutters up the thread and mysteriously, over time, the link to the image will
somehow become "unstable" or something to that effect. :) I can only surmise that is due to
Global Warming of the threads.

Saturday, November 16, 2013 4:48 PM

Reply | Quote
Mr. Monkeyboy Nothing in particular 77,215 Points

https://social.msdn.microsoft.com/Forums/vstudio/en-US/34ac95f5-4cad-4b11-937a-de2ee5896c04/trackbar-music-control?forum=vbgeneral 9/12
25/10/2018 Trackbar Music Control

0
Sign in
to vote

Okay. I tried a different song and it worked. Turns out something was wrong with the song I
picked -__-

Anyway, when I play a video I see that the player is shaded green over 75% of the box and it's
covering the video. Is it because the plugin is corrupt?

Bgeo99

Saturday, November 16, 2013 5:02 PM

Reply | Quote
Bgeo25 35 Points

1
Sign in
to vote

Okay. I tried a different song and it worked. Turns out something was wrong with the
song I picked -__-

Anyway, when I play a video I see that the player is shaded green over 75% of the box
and it's covering the video. Is it because the plugin is corrupt?

Bgeo99

I would have no idea since I have not had that issue before.

If I answered your question please propose my post as the answer.

Also this is how you would do the same thing without dropping the WMP control on your
Form. So you're just playing audio of whatever you want to listen to, even videos, but there is
a requirement to add more controls to the form since the WMP isn't displayed and you can't
access its controls.

Although there may be a parameter to make the WMP display for all I know and even set its
location on the form.

Public Class Form1

' Add reference, Project tab, add reference, Browse, C:\Windows\System3

Dim Media As New List(Of String)

Dim AxWindowsMediaPlayer1 As New WMPLib.WindowsMediaPlayer


Dim SomeMedia As WMPLib.IWMPMedia

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase


Me.CenterToScreen()
AxWindowsMediaPlayer1.windowlessVideo = True
AxWindowsMediaPlayer1.enableContextMenu = True
Media.Add("C:\Users\John\Desktop\Apollo 16.wmv")
Media.Add("C:\Users\John\Desktop\Pegasus.wmv")
Media.Add("C:\Users\John\Music\Miami Vice Theme.mp3")
Media.Add("C:\Users\John\Desktop\teddybearstereo.wav")
https://social.msdn.microsoft.com/Forums/vstudio/en-US/34ac95f5-4cad-4b11-937a-de2ee5896c04/trackbar-music-control?forum=vbgeneral 10/12
25/10/2018 Trackbar Music Control
ed a. dd( C:\Use s\Jo \ es top\teddybea ste eo. a )
Media.Add("C:\Users\John\Music\Phil_Collins_I_Dont_Care_Anymore.mp3
Media.Add("C:\Users\John\Music\freeway_jam.mp3")
Media.Add("C:\Users\John\Music\ZZ_Top__Viva_Las_Vegas.mp3")
For Each Item In Media
SomeMedia = AxWindowsMediaPlayer1.newMedia(Item)
AxWindowsMediaPlayer1.currentPlaylist.appendItem(SomeMedia)
Next
AxWindowsMediaPlayer1.settings.setMode("shuffle", True)
TrackBar1.Minimum = 0
Timer1.Interval = 100
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles But


AxWindowsMediaPlayer1.controls.playItem(SomeMedia)
Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer


TrackBar1.Maximum = CInt(AxWindowsMediaPlayer1.currentMedia.duratio
Label1.Text = AxWindowsMediaPlayer1.currentMedia.durationString
Label2.Text = AxWindowsMediaPlayer1.controls.currentPosition.ToStri
TrackBar1.Value = CInt(AxWindowsMediaPlayer1.controls.currentPositi
End Sub

Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles


AxWindowsMediaPlayer1.controls.pause()
AxWindowsMediaPlayer1.controls.currentPosition = CDbl(TrackBar1.Val
System.Threading.Thread.Sleep(300)
AxWindowsMediaPlayer1.controls.play()
End Sub
End Class

Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of
DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you
post an image it clutters up the thread and mysteriously, over time, the link to the image will
somehow become "unstable" or something to that effect. :) I can only surmise that is due to
Global Warming of the threads.

Edited by Mr. Monkeyboy Saturday, November 16, 2013 5:20 PM 5555

Marked as answer by Bgeo25 Saturday, November 16, 2013 11:49 PM

Unmarked as answer by Bgeo25 Saturday, November 16, 2013 11:50 PM

Saturday, November 16, 2013 5:19 PM

Reply | Quote
Mr. Monkeyboy Nothing in particular 77,215 Points

https://social.msdn.microsoft.com/Forums/vstudio/en-US/34ac95f5-4cad-4b11-937a-de2ee5896c04/trackbar-music-control?forum=vbgeneral 11/12
25/10/2018 Trackbar Music Control

0
Sign in
to vote

None of this doesn't work. All I get are exceptions unhandled.

Thursday, November 27, 2014 3:59 AM

Reply | Quote
Fusionultralight 0 Points

DEV CENTERS RELATED SITES CONNECT DEVELOPER RESOURCES


Windows Visual Studio Forums Code samples
Office Visual Studio Integrate Blog Documentation
More... VSIP Program Facebook Downloads
Microsoft .NET LinkedIn Products & extensions for Visual Studio
Microsoft Azure Stack Overflow REST APIs
Twitter Testing tools for web developers
Visual Studio Events Videos and tutorials
YouTube Virtual Labs

United States © 2018 Microsoft Terms of Use Trademarks Privacy Statement Site Feedback
(English)

https://social.msdn.microsoft.com/Forums/vstudio/en-US/34ac95f5-4cad-4b11-937a-de2ee5896c04/trackbar-music-control?forum=vbgeneral 12/12

You might also like