ASP: Export to M3U Playlist
Introduction
Author: Martin WarningYear: 2007
License: Free
This code shows an example on how to export a playlist of songs to the URL - Playlist format">M3U playlist format. This format is supported by many players The code examples provided are complete ASP documents. To use them just copy all the code into an ASP document, save it and you can try it out.
The key to this export is the our output is not an html document but an URL - Playlist format">M3U document. To do so we need to tell the browser what document type we are sending it. We use the ASP code Response.ContentType = "audio/mpegurl" to achieve this. Response.ContentType is used to identify a document as a different file type to the browser. The URL - Playlist format">M3U format is one of many document types you can create using ASP.
Code
Languages:- ASP
- VBScript
<%@ LANGUAGE="VBSCRIPT" %>
<% Option Explicit %>
<%
Response.ContentType = "audio/mpegurl"
Response.AddHeader "Content-Disposition","attachment; filename=MyPlaylist.m3u"
Response.Write("C:/My Music/Song.mp3" & VbCrLF)
Response.Write("C:/My Music/Song2.mp3" & VbCrLF)
Response.Write("C:/My Music/Song3.mp3")
%>
This code just creates a list with the locations to find the songs. VbCrLF is used as carriage return to create a new line. Response.AddHeader "Content-Disposition","attachment; filename=MyPlaylist.m3u" allows us to pre-define a filename when the user chooses to save the playlist. It also forces the open/save dialog so the user has the option to save the playlist or open it in their default player. If we take attachment out of this line (code: Response.AddHeader "Content-Disposition","filename=MyPlaylist.m3u") the playlist might be opened directly in the users default player depending on the browser settings.
The following code is an example extended M3U playlist export document:
<%@ LANGUAGE="VBSCRIPT" %>
<% Option Explicit %>
<%
Response.ContentType = "audio/mpegurl"
Response.AddHeader "Content-Disposition","attachment; filename=MyPlaylist.m3u"
Response.Write("#EXTM3U" & VbCrLF)
Response.Write("#EXTINF:200,Artist - Title" & VbCrLF)
Response.Write("C:/My Music/Song.mp3" & VbCrLF)
Response.Write("#EXTINF:150,Artist - Title" & VbCrLF)
Response.Write("C:/My Music/Song2.mp3")
%>
The code here is a concept of how to create an extended URL - Playlist format">M3U playlist. Extended playlists add some more information about the songs to the URL - Playlist format">M3U document. In the line Response.Write("#EXTINF:200,Artist - Title" & vbcrlf) the 200 is the length of the song in seconds and after the comma is the name of the song. The line Response.Write("C:/My Music/Song.mp3" & vbcrlf) is the location of the song. The last 2 lines of the code is something you could loop through. VbCrLF is used as carriage return to create a new line.
Further reading:
M3U (Wikipedia)
