VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



A Tutorial for Writing to / READING FROM An ASX playlist (the ones that Windows Media Player uses)

by Jon Barker (11 Submissions)
Category: Sound/MP3
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (5 Votes)

You can use the following code to read from, and write to ASX playlists, the type that can be read and created by Windows Media Player,
and generally most other music programs. This may not be the easyest code in the world to understand, so i would recommend you have a good knowlage of string manipulation before reading this (see my other uploads)
For the following code you work correctly, YOU MUST HAVE, ON THE FORM:
2 x Listboxes, name List1 and List2
1 x Textboxes, name Text1
tHe_cLeanER

Rate A Tutorial for Writing to / READING FROM An ASX playlist (the ones that Windows Media Player uses)

'For the following code you work correctly, YOU MUST HAVE, ON THE FORM:
'  2 x Listboxes, name List1 and List2
'  1 x Textboxes, name Text1
'--------------------------------------------------------------------
'
'To use the code, you call it similar to calling another other Sub or Function;
'
'(anywhere in your code)
'Call WriteToASX("C:\windows\newasx.asx") ' THIS WOULD WRITE TO THE FILENAME SPECIFIED
'
'or:
'
'(anywhere in your code)
'Call PullFromASX("C:\windows\newasx.asx") ' THIS WOULD READ FROM THE ASX FILE SPECIFIED
'
'------------------------ Creating an ASX file -----------------------
Sub WriteToASX(Path As String)
On Local Error GoTo WriteError ' ERROR HANDLING
Open Path For Output As #1 ' OPEN A NEW ASX FILE FOR WRITING TO
  Print #1, "" 'THE FOLLOWING CODE WRITE THE GENERAL HEADER INFO, SO THAT MEDIA PLAYER ACCEPTS IT
  Print #1, ""
  Print #1, "" 'SET THE FORMAT... AND CREDITS :)
  Print #1, "" 'INFO ABOUT THE PLAYLIST (REQUIRED)
  Print #1, ""
  For i = 0 To List1.ListCount - 1 'LOOP WRITING THE FILENAMES UNTIL ALL ARE DONE
  Print #1, ""
    Print #1, "" 'ONLY THE NAME OF THE SONG ETC, NOT PATH OR FILENAME
    Print #1, "" 'WRITES THE FULL PATH, AND FILENAME THAT ARE READ TO PLAY THE SONG
    Print #1, "
"
    Print #1, ""
  Next i
  Print #1, "" ' CLOSE THE ASX FILE
Close #1 'PHYSICALLY CLOSE THE OPEN FILE
Exit Sub
WriteError: ' AN ERROR HAS OCCURED; TELL THE USER, AND SAY WHAT THE ERROR IS
  MsgBox "There was an error writing to the playlist: " & Err.Description
End Sub
'---------------------------------------------------------------------
'
'This above code will write an ASX file that can be read by Windows Media Player. It uses List1 for the complete filenames for files you wish to be
'in the playlist, and reads from List2 the song names, or whatever you wish the filenames to be identified by.
'
'------------------------ Reading from a created ASX -----------------
Sub PullFromASX(Path As String)
Dim SStart, Getname As Long
Dim Part2 As Long             ' DECLARE THE VARIBLES
Dim FullFilename, CosmeticName As String
On Local Error GoTo errorasx 'BASIC ERROR HANDLING
SStart = 1  'WHERE TO START LOOKING FOR THE SONG
Getname = 1  'A TEMPORY INTEGER VALUE CONTAINING THE START OF THE SONG FILENAME
Open Path For Input As #1
  Text1.Text = Input(LOF(1), 1)  ' OPEN THE EXISTING ASX FILE, AND EXTRACT THE CONTENTS TO TEXT1
Close #1
Do Until Getname = "0"
  Getname = InStr(SStart, Text1.Text, "  Part2 = InStr(Getname + 13, Text1.Text, Chr(34)) ' FIND THE END OF THE SONG FILENAME
  FullFilename = Mid(Text1.Text, Getname + 13, Part2 - Getname - 13) ' FIND THE FILE NAME, RELATIVE TO FIRST AND LAST PARTS
  
  If Getname <> 0 Then List1.AddItem FullFilename ' ADD THE FILENAME TO LIST1
  SStart = Getname + 1 ' SPECIFIES WHERE TO SEARCH FROM (AND UPDATES IT)
Loop ' RESTART THE LOOP
MsgBox "All filenames extracted from the ASX file. Continuing to extract song names."
Call PullCosmeticsASX(Path) ' START THE NEXT STEP
Exit Sub
errorasx: ' AN ERROR HAS OCCURED; TELL THE USER, AND SAY WHAT THE ERROR IS
  MsgBox "There was an error reading from the playlist: " & Err.Description
End Sub


Sub PullCosmeticsASX(Path As String)
Dim SStart, Getname As Long
Dim Part2 As Long             ' DECLARE THE VARIBLES
Dim FullFilename, CosmeticName As String
On Local Error GoTo errorasx 'BASIC ERROR HANDLING
SStart = 1
Getname = 1
Open Path For Input As #1
  Text1.Text = Input(LOF(1), 1)  ' OPEN THE EXISTING ASX FILE, AND EXTRACT THE CONTENTS TO TEXT1
Close #1
Do Until Getname = 0 ' START THE LOOP, LOOKING FOR SONG NAMES
  Getname = InStr(SStart, Text1.Text, "  
  Part2 = InStr(Getname + 30, Text1.Text, Chr(34)) ' FIND THE END OF THE NAME
  CosmeticName = Mid(Text1.Text, Getname + 30, Part2 - Getname - 30) ' FIND THE SONG NAME, RELATIVE TO FIRST AND LAST PARTS
  
  If Getname <> 0 Then List2.AddItem CosmeticName ' ADD THE SONG NAME TO LIST2
  SStart = Getname + 1 ' SPECIFIES WHERE TO SEARCH FROM (AND UPDATES IT)
Loop ' RESTART THE LOOP
MsgBox "Completed reading from playlist."  ' IF NO MORE ENTERIES ARE FOUND THEN YOU ARE FINISHED; EXIT SUB
Exit Sub
errorasx: ' AN ERROR HAS OCCURED; TELL THE USER, AND SAY WHAT THE ERROR IS
  MsgBox "There was an error reading from the playlist: " & Err.Description
End Sub
'---------------------------------------------------------------------------------
'
'The above code will do two things:
'  1. Take all the full filenames of songs etc in the ASX file, and add them to List1
'  2. Take just the filename, or name of song, or whatever is specified in the ASX, and put it into List2
'Text1 just holds the ASX file to read from it, and is only used when directly reading from the ASX file.
'
'---------------------------------------------------------------------------------
Hope this helps you to understand the making of the playlist, and the various string manipulation commands.
If you need a detailed explanation of the InStr command etc, then check out my other tutorials 
(click 'other submissions from this person' below)

keep coding!

tHe_cLeanER
[email protected]

Download this snippet    Add to My Saved Code

A Tutorial for Writing to / READING FROM An ASX playlist (the ones that Windows Media Player uses) Comments

No comments have been posted about A Tutorial for Writing to / READING FROM An ASX playlist (the ones that Windows Media Player uses). Why not be the first to post a comment about A Tutorial for Writing to / READING FROM An ASX playlist (the ones that Windows Media Player uses).

Post your comment

Subject:
Message:
0/1000 characters