Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Windows Media Player meets Shoutcast

0.00/5 (No votes)
6 Jul 2008 1  
How to play *.pls internet radios / files in Windows Media Player
Get it from CNET Download.com!

Introduction

Let's face it Windows Media Player looks sexy but what is more important it sounds a lot better than any other overskinned overpluggined monster out there. At least for me ;) Now it can't open pls files but can play any stream addresses contained in them due to some weird marketing decission. So the sollution is to get url from pls file and pass it to the player as commandline parameter

So here is simple utility to open and play internet streams or files contained in popular *.pls playlist format in Windows Media Player by simply clicking on interned radios in web browser.

How to Use

When you click on any radio on internet radio sites like www.shoutcast.com web browser usually asks in which program to open *.pls files. Just browse to pls2wmp.exe utility downloaded from this page (or compile it from provided source code) and mark Allways use check box. But if browser doesn't ask it usually means that pls file is associated with different program. In that case Hold Shift and right click on any downloaded pls file. -> Open with -> Choose Program -> browse for and select pls2wmp.exe -> check Always use selected .... checkbox. And voila all internet radios now open Windows Media Player.

The Source Code

Well the code si very simple. It opens pls file and passes first found url/file path to launched windows media player as parameter.

To make it little bit less boring it demonstrates how to read and process files without usual check file size -> allocate -> copy memory mantra. Windows does all for us. How it works? Everytime we first time touch the page sized memory (4096 bytes) via pointer returned from MapViewOfFile windows internaly generates exception that allocates page -> copies data from file. But this is all transparent to us so we just read this pointer and let the windows do the dirty job. Another advantage of this approach is that only parts of file that are accessed are allocated/transfered. So offset based operations on multi gigabyte files are extremly fast. I used PAGE_WRITECOPY which means that if we try to modify data windows allocates another temporary memory where he holds just changes without writing changes back to file.

But main purpose of this article is to share new way of listening to internet radios on sites like www.sky.fm or www.shoutcast.com or www.live365.com also in Windows Media Player.

That's it format it the way you like it and enjoy the better sounding music. ;)
#include <windows.h>
#include <stdio.h>

CALLBACK WinMain( HINSTANCE inst, HINSTANCE prev, char* cmd, int show ) {   
    int len = strlen(cmd);
    if(!len) return -1; cmd++;
    if(cmd[len-2]=='"') cmd[len-2]=0;

    HANDLE file = CreateFile(cmd,GENERIC_READ,1,0,OPEN_EXISTING,0,0); if(file==INVALID_HANDLE_VALUE) return -1;
    HANDLE map  = CreateFileMapping(file,0,PAGE_WRITECOPY,0,10,0);    if(!map) return -1;
    char*  url  = (char*)MapViewOfFile(map,1,0,0,0);                  if(!url) return -1;
           url  = strstr(url,"ile");                                  if(!url) return -1;
           url  = strchr(url,'='  );                                  if(!url) return -1;
    char*  end  = strchr(url,'\n' );                                  if( end) *end = 0;

    ShellExecute(0,"open","wmplayer",url+1,0,SW_SHOW);
    UnmapViewOfFile(url);

    CloseHandle(file);
    CloseHandle(map);
    return 0;
}
     

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here