yuwery

i used Windows Media control to play sound files as .mp3, .wma...and create a playlist.
i want to show the playlist in windows media player!
how can i do that

thanks for every answer!


Re: Visual Basic General how to show the playlist in windows media player

Bruno Yu - MSFT

yuwery,

The following code shows a working playlist for Windows Media Player, please add the code in your application:

Code Snippet

Imports System.IO

Public Class Form1

' NOTES:

' Form1 contains a WindowsMediaPlayer (named AxWMP1)

' Form1 contains a Listbox (named LB1)

' Form1 contains a ContextMenuStrip (named CMS1)

' LB1's .ContextMenuStrip is pointing to CMS1

' WMP's .currentMedia => setting this creates a new playlist with a single item

' WMP's .Ctlcontrols.currentItem => sets the currentItem to a track from the currentPlaylist

Private WithEvents zTimer As New Windows.Forms.Timer

Private zUpdatedSelectedIndex As Integer = -1

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

With zTimer

.Interval = 100

.Enabled = True

.Start()

End With

Me.Init_CMS1()

End Sub

Private Sub Init_CMS1()

CMS1.Items.Clear()

CMS1.Items.Add(New ToolStripMenuItem("Select New Folder", Nothing, AddressOf SelectFolderToolStripMenuItem_Click))

CMS1.Items.Add(New ToolStripSeparator)

End Sub

Private Sub SelectFolderToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)

Static zSelectedFolder As String = "c:\"

Dim zRightClickLocation As Point = System.Windows.Forms.Cursor.Position

Dim zFBD As New FolderBrowserDialog

With zFBD

.Description = "Select new folder to load playlists from"

.SelectedPath = zSelectedFolder

.ShowNewFolderButton = False

.ShowDialog(Me)

End With

zSelectedFolder = zFBD.SelectedPath

Me.CreatePlayListItems(zSelectedFolder)

CMS1.Show(zRightClickLocation)

End Sub

Private Sub CreatePlayListItems(ByVal zGivenFolder As String)

' Get all the playlists from the given folder

Dim zPlayListFiles As New List(Of String)(My.Computer.FileSystem.GetFiles(zGivenFolder, FileIO.SearchOption.SearchTopLevelOnly, New String() {"*.wpl", "*.pl", "*.m3u", "*.asx"}))

' Clear the contextmenustrip

Me.Init_CMS1()

Dim zPlayList As WMPLib.IWMPPlaylist

Dim zPlayListMenuItem As ToolStripMenuItem = Nothing

Dim zTrack As WMPLib.IWMPMedia3

Dim zTrackMenuItem As ToolStripMenuItem = Nothing

For Each zFilePath As String In zPlayListFiles

zPlayList = AxWMP1.newPlaylist(Path.GetFileName(zFilePath), zFilePath)

zPlayListMenuItem = New ToolStripMenuItem(zPlayList.name, Nothing, AddressOf PlayListToolStripMenuItem_Click)

zPlayListMenuItem.Tag = zPlayList

zPlayListMenuItem.Owner = CMS1

If zPlayList.count > 0 Then

Dim zListOfTracks As New List(Of ToolStripMenuItem)

For zIndex As Integer = 0 To zPlayList.count - 1

zTrack = zPlayList.Item(zIndex)

zTrackMenuItem = New ToolStripMenuItem(zTrack.name, Nothing, AddressOf TrackToolStripMenuItem_Click)

zTrackMenuItem.Tag = zIndex

zListOfTracks.Add(zTrackMenuItem)

Next

zPlayListMenuItem.DropDownItems.AddRange(zListOfTracks.ToArray)

End If

Next

End Sub

Private Sub PlayListToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)

' When a playlist is clicked, set the parent playlist as the current playlist and start the first track

Dim zPlayListMenuItem As ToolStripMenuItem = sender

Dim zPlayList As WMPLib.IWMPPlaylist = zPlayListMenuItem.Tag

If zPlayList.isIdentical(AxWMP1.currentPlaylist) Then

' don't do anything

Else

' load the new playlist

AxWMP1.currentPlaylist = zPlayList

If zPlayList.count > 0 Then

' Load playlist into LB1 (=ListBox) by putting them all in a list first and then adding to the listbox (->faster)

LB1.Items.Clear()

Dim zListOfTracks As New List(Of String)

Dim zSelectedTrackIndex As Integer = 0

For zIndex As Integer = 0 To zPlayList.count - 1

' Note: after pulling out the hair on my head (and chest), I'm now assuming that the WMP doesn't

' provide its own PlaylistIndex to keep track of the current item, so I'm adding one myself.

zPlayList.Item(zIndex).setItemInfo("PlaylistIndex", zIndex.ToString)

zListOfTracks.Add((zIndex + 1).ToString("00") & ") " & zPlayList.Item(zIndex).name & " (" & zPlayList.Item(zIndex).durationString & ")")

Next

LB1.Items.AddRange(zListOfTracks.ToArray)

LB1.SelectedIndex = 0

End If

' close the contextmenustrip

CMS1.Close(ToolStripDropDownCloseReason.ItemClicked)

End If

End Sub

Private Sub TrackToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)

' When a track is clicked, set the parent playlist as the current playlist and select the track to play

Dim zTrackMenuItem As ToolStripMenuItem = sender

Dim zPlayListMenuItem As ToolStripMenuItem = zTrackMenuItem.OwnerItem

Dim zPlayList As WMPLib.IWMPPlaylist = zPlayListMenuItem.Tag

Dim zTrack As WMPLib.IWMPMedia3 = zPlayList.Item(zTrackMenuItem.Tag)

Dim zSelectedTrackIndex As Integer = -1

If zPlayList.isIdentical(AxWMP1.currentPlaylist) Then

' just set ListBox's SelectedIndex to the selected track

[Integer].TryParse(zTrack.getItemInfo("PlaylistIndex"), zSelectedTrackIndex)

LB1.SelectedIndex = zSelectedTrackIndex

Else

' load the new playlist and set the selected track

AxWMP1.currentPlaylist = zPlayList

' Load playlist into LB1 (=ListBox) by putting them all in a list first and then adding to the listbox (->faster)

LB1.Items.Clear()

Dim zListOfTracks As New List(Of String)

For zIndex As Integer = 0 To zPlayList.count - 1

' Note: after pulling out the hair on my head (and chest), I'm now assuming that the WMP doesn't

' provide its own PlaylistIndex to keep track of the current item, so I'm adding one myself.

zPlayList.Item(zIndex).setItemInfo("PlaylistIndex", zIndex.ToString)

zListOfTracks.Add((zIndex + 1).ToString("00") & ") " & zPlayList.Item(zIndex).name & " (" & zPlayList.Item(zIndex).durationString & ")")

If zPlayList.Item(zIndex).sourceURL = zTrack.sourceURL Then

zSelectedTrackIndex = zIndex

End If

Next

LB1.Items.AddRange(zListOfTracks.ToArray)

' Setting the Listbox's SelectedIndex will also set the WMP's Ctlcontrols.currentItem

LB1.SelectedIndex = zSelectedTrackIndex

End If

End Sub

Private Sub LB1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles LB1.SelectedIndexChanged

' when an item in the listbox is clicked, set that track as the currentitem

If LB1.SelectedIndex > -1 Then

AxWMP1.Ctlcontrols.currentItem = AxWMP1.currentPlaylist.Item(LB1.SelectedIndex)

AxWMP1.Ctlcontrols.playItem(AxWMP1.Ctlcontrols.currentItem)

End If

End Sub

Private Sub AxWMP1_CurrentItemChange(ByVal sender As Object, ByVal e As AxWMPLib._WMPOCXEvents_CurrentItemChangeEvent) Handles AxWMP1.CurrentItemChange

Dim zNewTrack As WMPLib.IWMPMedia3 = e.pdispMedia

If zNewTrack IsNot Nothing Then

' Note: I'm using zTimer and the zUpdatedSelectedIndex variable to set the new listbox.selectedindex because

' there are LoaderLock errors if you try to update the managed Listbox directly within this eventsub

Dim zSelectedIndex As Integer = -1

[Integer].TryParse(zNewTrack.getItemInfo("PlaylistIndex"), zSelectedIndex)

zUpdatedSelectedIndex = zSelectedIndex

End If

End Sub

Private Sub zTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles zTimer.Tick

If zUpdatedSelectedIndex > -1 Then

LB1.SelectedIndex = zUpdatedSelectedIndex

zUpdatedSelectedIndex = -1

End If

End Sub

End Class