Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,605 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 932 people online right now. Registration is fast and FREE... Join Now!




MyMusic Player

 
Reply to this topicStart new topic

> MyMusic Player, How to implement a simple MP3 Player

AdamSpeight2008
Group Icon



post 12 Jul, 2008 - 06:46 AM
Post #1


MyMusic Player

Modification: You'll need to add a COM Reference Windows Media Player (the one with path that ends InterOp.WMPLib.dll)

Start a new Windows Form Application project
Ingredients
3 x Buttons
But_Play
But_Pause
But_Stop
2 x Textboxes
Txt_TrackName
Txt_Progress
These need to be readonly
2 x TrackBar
TrackPosition
Volume (Vertical orientation, Minimum =0. Maximum=100)
1 x Timer
Timer1
1 x Listview (Multi Select = False, View=Details, HeaderStyle None)
Add one column called TrackCol


The code
View the code for Form1 and insert the following code

vb

Public Class Form1
#Region "Color Settings"
Dim CurrentTrackColor As System.Drawing.Color = Color.Red
Dim PausedTrackColor As System.Drawing.Color = Color.LightYellow
#End Region
Dim WithEvents Player As New WMPLib.WindowsMediaPlayer
Dim files As Collections.ObjectModel.ReadOnlyCollection(Of String)
Dim titles As New List(Of String)
Dim CurrentPlaying As Integer = 0
Dim PreviouslyPlaying As Integer = 0

Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
' Dispose of player
Player = Nothing
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TrackCol.Width = TrackList.Width - 20
But_Pause.Enabled = False
But_Stop.Enabled = False
files = FileIO.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyMusic, FileIO.SearchOption.SearchAllSubDirectories, "*.mp3")
For Each a As String In files
Me.TrackList.Items.Add(FileIO.FileSystem.GetName(a))
Next
Volume.Value = Player.settings.volume
Me.Txt_TrackName.Text = Player.URL
Player.settings.autoStart = False
Player.URL = files(0)
Player.enableContextMenu = False
With Me.Timer1
.Interval = 500
.Start()
.Enabled = True
End With
End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
' Form is closing, so shutdown player
Player.close()
End Sub

Private Sub ClickedOnPlayButton(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_Play.Click
GUIMode("Play")
updatePlayer()
Player.controls.play()
End Sub

Private Sub ClickedOnStopNutton(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_Stop.Click
Player.controls.stop()
GUIMode("Stopped")
End Sub

Private Sub ClickedOnPauseButton(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_Pause.Click
If Player.playState = WMPLib.WMPPlayState.wmppsPaused Then
GUIMode("Play")
Else
GUIMode("Paused")
End If
End Sub

Private Sub GUIMode(ByRef Guimode As String)
Select Case Guimode
Case "Play"
' Put GUI in playing mode guise
Player.controls.play()
But_Pause.BackColor = System.Drawing.SystemColors.Control
But_Pause.Enabled = True
But_Stop.Enabled = True
But_Play.Enabled = True
Case "Paused"
' put gui in paused mode guise
But_Pause.Enabled = True
But_Stop.Enabled = False
But_Play.Enabled = False
But_Pause.BackColor = PausedTrackColor
Player.controls.pause()
Case "Stopped"
But_Pause.Enabled = False
But_Stop.Enabled = False

End Select
End Sub

Private Sub ScrollingVolume(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Volume.Scroll
' Change the player's volume
Player.settings.volume = Volume.Value
End Sub

Private Sub Player_MediaError(ByVal pMediaObject As Object) Handles Player.MediaError
MessageBox.Show("Unrecoverable Problem. Shutting Down", "MyMusic Player")
Me.Close()
End Sub

Private Sub Player_PlayStateChange(ByVal NewState As Integer) Handles Player.PlayStateChange
Static Dim PlayAllowed As Boolean = True
Select Case CType(NewState, WMPLib.WMPPlayState)
Case WMPLib.WMPPlayState.wmppsReady
If PlayAllowed Then
Player.controls.play()
End If
Case WMPLib.WMPPlayState.wmppsMediaEnded
' Reach end of track move onto next, looping around
PreviouslyPlaying = CurrentPlaying
CurrentPlaying = (CurrentPlaying + 1) Mod files.Count
' Start protection (without it next wouldn't play
PlayAllowed = False
' Play track
Player.URL = files(CurrentPlaying)
Player.controls.play()
' End Protection
PlayAllowed = True
updatePlayer()
End Select

End Sub

Private Sub updatePlayer()
' Display track name
Txt_TrackName.Text = Player.currentMedia.name
' Update TrackPostion
With TrackPosition
.Minimum = 0
.Maximum = CInt(Player.currentMedia.duration)
.Value = CInt(Player.controls.currentPosition())
End With
' Display Current Time Position and Duration
Txt_Progress.Text = Player.controls.currentPositionString & vbTab & Player.currentMedia.durationString
' Set Volume slide to match current volume
Volume.Value = Player.settings.volume
' Is the CurrentPlaying Track No. is different to the Previous Track number.
If CurrentPlaying <> PreviouslyPlaying Then
' Yes,
' Set the forecolor of the corrisponding track, assiociated with the previous playing track, with the control color
TrackList.Items(PreviouslyPlaying).ForeColor = System.Drawing.SystemColors.ControlText
End If
' Set the forecolor of the corrisponding track, assiociated with the currently playing track, with the current track color
TrackList.Items(CurrentPlaying).ForeColor = CurrentTrackColor

End Sub

Private Sub Tracks_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TrackList.MouseDoubleClick
GUIMode("Play")

' A track in the tracklisting has been double clicked on
PreviouslyPlaying = CurrentPlaying
' Set CurrentPlaying to position of selected track.
CurrentPlaying = TrackList.SelectedIndices(0)
' Play the track
Player.URL = files(CurrentPlaying)
updatePlayer()
Player.controls.play()
End Sub

Private Sub ScrollingTrackPosition(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackPosition.Scroll
' Seek the through track
Player.controls.pause()
Player.controls.currentPosition = TrackPosition.Value
Player.controls.play()
updatePlayer()
' Allow the app to do some processing
Application.DoEvents()
End Sub

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

End Class


Bake in a preheated over a gas mark 6 for 1 hour.

Here one I made earlier
Attached File  MediaPlayer.zip ( 923.23k ) Number of downloads: 356

Run

Now enjoy!

This post has been edited by AdamSpeight2008: 15 Jul, 2008 - 03:03 AM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

jagatworld
Group Icon



post 14 Jul, 2008 - 11:07 PM
Post #2
Hi,
Everything looks fine, but only for that , I'm unable to find this library / control ......

Dim WithEvents Player As New WMPLib.WindowsMediaPlayer
Error : Type 'WMPLib.WindowsMediaPlayer' is not defined.

it is giving error.
Should I import some control or add any reference?

I use VS 2005 Team Suite.

Thanks and Regards.

Jagat.
Go to the top of the page
+Quote Post

AdamSpeight2008
Group Icon



post 15 Jul, 2008 - 03:02 AM
Post #3
QUOTE(jagatworld @ 15 Jul, 2008 - 08:07 AM) *

Hi,
Everything looks fine, but only for that , I'm unable to find this library / control ......

Dim WithEvents Player As New WMPLib.WindowsMediaPlayer
Error : Type 'WMPLib.WindowsMediaPlayer' is not defined.

it is giving error.
Should I import some control or add any reference?

I use VS 2005 Team Suite.

Thanks and Regards.

Jagat.


You'll need to add a COM Reference Windows Media Player (the one with path that ends InterOp.WMPLib.dll)
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/23/08 02:17AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month