11 Replies - 11297 Views - Last Post: 05 October 2008 - 09:18 PM Rate Topic: -----

#1 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

WMP Playlist

Posted 04 October 2008 - 09:59 AM

I have a WMPLib.WindowsMediaPlayer on my form, and I want to have a playlist for it. All I can get it to do now is play one song, then stop.

I just need to know how to create a playlist (I think I accomplished this last night, but it was like 3 in the morning, so I don't remember), and play it using the buttons on the WMP control.

If you need my code, I'll post it. However, it's only a few lines of code. Matter of fact, I'll just post it now...:D

Public Class Form1

    Dim playlist As Collections.ObjectModel.ReadOnlyCollection(Of String)

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

        playlist = FileIO.FileSystem.GetFiles(My.Computer.FileSystem.CurrentDirectory, FileIO.SearchAllSubDirectories, ".mp3")
        Me.player.URL = playlist(0)

    End Sub

End Class


I understand that the playlist is not really a "playlist". It's just a collection of songs. But I want to make it so I can hit the "Next" button on the WMP control, and it plays the next song in the list.

Thanks in advance!

This post has been edited by Locke37: 04 October 2008 - 10:19 AM


Is This A Good Question/Topic? 0
  • +

Replies To: WMP Playlist

#2 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: WMP Playlist

Posted 04 October 2008 - 10:31 AM

You could have the playlist be a XML File, like so:
<?xml version="1.0" encoding="utf-8" ?>
<Playlist>
  <Songs>
    <Song title="Song1" artist="Artist1" url="Url1" />
    <Song title="Song2" artist="Artist2" url="Url2" />
    <Song title="Song3" artist="Artist3" url="Url3" />
    <Song title="Song4" artist="Artist4" url="Url4" />
    <Song title="Song5" artist="Artist5" url="Url5" />
  </Songs>
</Playlist>


Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: WMP Playlist

Posted 04 October 2008 - 11:48 AM

I don't know crap about XML...:unsure:
Was This Post Helpful? 0
  • +
  • -

#4 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: WMP Playlist

Posted 04 October 2008 - 11:49 AM

But do you know how to make a XML Reader / Writer in VB.NET?
Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: WMP Playlist

Posted 04 October 2008 - 11:56 AM

uhhh...no :D

VB.Net isn't what I'm really best at. But I got an urge last night to expand my knowledge. :)

This post has been edited by Locke37: 04 October 2008 - 11:56 AM

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: WMP Playlist

Posted 04 October 2008 - 12:11 PM

This is how I did it in the player I created last year. I had a class named MediaUtilities with a method named CreatePlayList. Here is how it worked. In my button click event I had

Private Sub cmdCreate_Click(ByVal sender As Object, ByVal e As EventArgs)
	'extensions we want to load
	Dim extensions As String() = {".mp3", ".wma", ".mp4", ".wav"}
	Dim songs As New Hashtable()
	'loop through each node in the TreeView

	'now that we have our songs in the Hashtable, build the playlist
	media.List = songs
	media.PlaylistName = txtName.Text
	media.PlaylistDirectory = New DirectoryInfo(Properties.Settings.[Default].PlaylistDirectory)
	media.CreatePlaylist()
End Sub



As you can imagine PlaylistName, PlaylistDirectory are public properties of the MediaUtilities class. Here is the CreatePlaylist method

make sure the file name doesnt already exist
If Not (File.Exists(_playlistName)) Then
	'file doesnt exist so we can start creating the new playlist
	writer = New XmlTextWriter(_playlistDir + "\" + _playlistName + ".wpl", Nothing)
	writer.WriteProcessingInstruction("wpl", "version=""1.0""")
	writer.WriteStartElement("smil")
	'write the head of our document
	WriteHead()
	writer.WriteStartElement("body")
	writer.WriteStartElement("seq")
	'Now add the songs the users selected
	AddTracks()
	'end the document
	writer.WriteEndElement()
	writer.WriteEndElement()
	writer.WriteEndElement()
	'close the writer object
	writer.Close()

Else
	MessageBox.Show("A playlist with the name " & _playlistName + " already exists!")
End If



Here is the WriteHead method

Private Sub WriteHead()
	writer.WriteStartElement("head")
	writer.WriteElementString("title", _playlistName)
	writer.WriteEndElement()
End Sub



And here is the AddTracks method that is called

Private Sub AddTracks()
	'variable to hold the full path of the track to be added
	Dim track As String = String.Empty
	'now we begin our loop
	For i As Integer = 0 To _list.Count - 1
		'build our track listing in the format
		'file://C:\Folder\Artist\Album\Track.Extension
		track = _list(i).ToString()
		'now add the track
		writer.WriteStartElement("media")
		writer.WriteAttributeString("src", "file://" + track)
		writer.WriteEndElement()
	Next
End Sub



The writer variable is a global at the top, and _list is the property variable for the public Hashtable property of the MediaUtilities class

Private writer As XmlTextWriter



Private _list As Hashtable
Private _playlistDir As DirectoryInfo
Private _playlistName As String

''' <summary>
''' property to hold the Playlist Object populated with the songs to
''' be inserted into the playlist
''' </summary>
Public Property List() As Hashtable
	Get
		Return _list
	End Get
	Set
		_list = value
	End Set
End Property

''' <summary>
''' property to hold the directory for the playlists
''' </summary>
Public Property PlaylistDirectory() As DirectoryInfo
	Get
		Return _playlistDir
	End Get
	Set
		_playlistDir = value
	End Set
End Property

''' <summary>
''' property to hold the name of the Playlist being created
''' </summary>
Public Property PlaylistName() As String
	Get
		Return _playlistName
	End Get
	Set
		_playlistName = value
	End Set
End Property



Since I dont know where you're getting your song names from I removed the part in the button click where I looped through my TreeView that I populated by dragging files from the main TreeView (that holds my music library). Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: WMP Playlist

Posted 04 October 2008 - 01:08 PM

@PsychoCoder, in your CreatePlaylist method...I get this error...on the shown line.

writer = new XMLTextWriter(_playlistDir + "\" + _playlistName + "wpl", Nothing)

' it says "Operator '+' is not defined for types 'System.DirectoryInfo'
' and 'String'."


Any ideas/fixes?

EDIT: Nevermind, got that one figured out.

But how would I get the files into my hashtable like you did? The equivalent of you looping thru your nodes, but I have a specific directory that I wanna search in.

See I plan on carrying this around with me on a thumb drive. So I just need to search thru the current directory, AKA the drive, for the mp3 files I'll have on it.

This post has been edited by Locke37: 04 October 2008 - 01:21 PM

Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: WMP Playlist

Posted 04 October 2008 - 05:17 PM

Here is a way to loop through your My Music folder

Dim root As New DirectoryInfo(Environment.SpecialFolder.MyMusic)
Dim song As New Hashtable()
Dim extensions As String() = {".mp3", ".wma", ".mp4", ".wav"}  
For Each artistDir As DirectoryInfo In root.GetDirectories()
	'loop thorugh each top level directory (artists) to get all it's subdirectories (albums)
	For Each albumDir As DirectoryInfo In artistDir.GetDirectories()
		'loop through each subdirectory (albums) getting the files in each (songs)
		For Each song As FileInfo In albumDir.GetFiles()
			For i As Integer = 0 To extensions.Length - 1
				'loop through our extensions array
				'make sure the file has an extension we're looking for
				If song.Name.EndsWith(extensions(i)) Then
					song.Add(song.Name,nothing)
				End If
			Next
		Next
	Next
Next


Was This Post Helpful? 0
  • +
  • -

#9 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: WMP Playlist

Posted 04 October 2008 - 11:09 PM

View PostPsychoCoder, on 4 Oct, 2008 - 05:17 PM, said:

Here is a way to loop through your My Music folder

Dim root As New DirectoryInfo(Environment.SpecialFolder.MyMusic)
Dim song As New Hashtable()
Dim extensions As String() = {".mp3", ".wma", ".mp4", ".wav"}  
For Each artistDir As DirectoryInfo In root.GetDirectories()
	'loop thorugh each top level directory (artists) to get all it's subdirectories (albums)
	For Each albumDir As DirectoryInfo In artistDir.GetDirectories()
		'loop through each subdirectory (albums) getting the files in each (songs)
		For Each song As FileInfo In albumDir.GetFiles()
			For i As Integer = 0 To extensions.Length - 1
				'loop through our extensions array
				'make sure the file has an extension we're looking for
				If song.Name.EndsWith(extensions(i)) Then
					song.Add(song.Name,nothing)
				End If
			Next
		Next
	Next
Next



Right, but now how do I play that playlist? :unsure:
Was This Post Helpful? 0
  • +
  • -

#10 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: WMP Playlist

Posted 04 October 2008 - 11:26 PM

OK new theory...I just want to get at the little "Next" button, on my WindowsMediaPlayer control, from the code.

If I can get to that...I can handle it from there.

I just can't seem to find it anywhere.

This post has been edited by Locke37: 04 October 2008 - 11:27 PM

Was This Post Helpful? 0
  • +
  • -

#11 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: WMP Playlist

Posted 05 October 2008 - 12:09 AM

Ok new theory again...I got my playlist to play...but now it keeps repeating the first track like it's the only one on there.

When I hit "Next", it acts like I hit "Stop". It acts like there's only 1 track in the playlist.

EDIT: Nevermind...got it

I think that does it for the help I needed. Thanks a lot ! :D

This post has been edited by Locke37: 05 October 2008 - 12:10 AM

Was This Post Helpful? 0
  • +
  • -

#12 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: WMP Playlist

Posted 05 October 2008 - 09:18 PM

Ok, new problem, I have tried testing my program on my roommate's computer, and it doesn't find the playlist. I've made sure that it's included with the files.

I just need to somehow find the playlist in the applications directory (basically, the current directory) and send it to the player, but when I get the current directory, it's always just a path string.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1