|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
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;
}
| You must Sign In to use this message board. |
|
| | Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh) | FirstPrevNext |
|
|
 |
|
|
Great Program!! Was wondering if you could add ID3 support to the pls2wmp.exe, so when playing it in WMP the Song and Artist info is also shown.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Everything you write, you boast about how few lines you can do it in. Who cares? You put, what should be in 2 lines, as one line. Such as the following code:
HANDLE file = CreateFile(cmd,GENERIC_READ,1,0,OPEN_EXISTING,0,0); if(file==INVALID_HANDLE_VALUE) return 0;
This does not make you a good programmer. But makes your code difficult to read. Good programmers arent judged by how few lines they write, but by how reliable, functional and commercially valuable their code is.
Secondly, in your code you return a ZERO(0) on all function failures. Fine! But at the end of the code, where presumably everything has executed well, you still return a ZERO (0).
You see what i mean now? concentrate on writing good code; and not how few lines you can do it in.....
you got alot to learn, kid.

|
| Sign In·View Thread·PermaLink | 3.00/5 (2 votes) |
|
|
|
 |
|
|
Hi Jim . well this article is not about number of lines. It's about new way of listening to intenet radios. But back to returned error codes. They are useless remnants from pre-DOS days. If you feel like implementing features that nobody uses ... well nothing holds you. About formatting. I'ts no longer 1970 and developers have monitors that have 2 dimensions. Now is realy code where everything is put on new line so you must nonstop scroll on widescreen monitors more readable than this code ? I belive in something called important and nonimportant code parts. And that if there is way to keep more important code together but symetric whole article is a lot more readable. Consider code from my old article http://www.codeproject.com/KB/audio-video/VideoImageGrabber.aspx[^]where nearly every new line required new variable. How readable it would become if I didn't put more statements on one line ? Concerning small code vs class overbloat. And your satement that "who cares". Did you ever wondered how come that installed windows are slower than old ones and that they take 5gb=5 times as much space on disk just to look more polished ?. How come that you have 2 dualcore 3ghz processors yet some absolutely trivial tasks freeze computer on 5-10s ? Or that developers just can't keep up with more and more bugfixes and security holes? It's all in the develop fast forget fast developer culture . But while there will be programmers who "do not care". There will be need for more and more Ghz memory and security software covering bugs in bloatware they produce.
"There is always a better way"
modified on Sunday, July 6, 2008 7:22 PM
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
Ladislav Nevery wrote: But back to returned error codes. They are useless remnants from pre-DOS days.
No they are not. They a restriction based on the language you use. You can throw an exception in C. Nor can you in Pascal. Nor in numerous other procedural languages. Furthermore, he does have a valid point. Returning 0 from a main entry point function is considered a successful return. So why not just return a -1? That's considered an error.
Ladislav Nevery wrote: Now is realy code where everything is put on new line so you must nonstop scroll on widescreen monitors more readable than this code ?
Yes it is. I had the same reaction the OP had as well. It's extremely hard to read your code and understand what's happening. Case in point:
char* url = (char*)MapViewOfFile(map,1,0,0,0),*end; if(!url) return 0; url = strstr(url,"ile"); if(!url) return 0; url = strchr(url,'=' ); if(!url) return 0; end = strchr(url,'\n' ); if( end) *end = 0;
The "if (!url)" isn't so bad, but the problem is a few lines down. Where did "end" come from? What is it? I looked and looked and then I saw where you'd declared it, all in one line with an assignment (and on top of which you're not initializing the variable which is just poor form anyways). That's just really, really hard to read. And it doesn't take up any more or less code to just write:
char* url = (char*)MapViewOfFile(map,1,0,0,0); char* end = NULL; if(!url) return -1;
Adding a few carriage returns doesn't cause code bloat.
Ladislav Nevery wrote: I belive in something called important and nonimportant code parts.
Fantastic, good to hear it. Use things like white space to indicate this. Use comments to help the poor developer who has to come along after you figure out what is important and what isn't.
Ladislav Nevery wrote: Consider code from my old article http://www.codeproject.com/KB/audio-video/VideoImageGrabber.aspx[^]where nearly every new line required new variable. How readable it would become if I didn't put more statements on one line ?
A hell of a lot more readable than it currently is. Which is too bad because it clearly does some interesting stuff.
Ladislav Nevery wrote: Concerning small code vs class overbloat. And your satement that "who cares".
His statement "who cares" was regarding your emphasis on not using line breaks in you code. No mention of "class overbloat" (whatever that actually means) is mentioned. Skipping carriage returns has NO effect on the final size of the executable, since it's whitespace and get's dumped by the compiler in it's parse phase.
All of this aside, the larger issue I personally had with your article was this statement:
Ladislav Nevery wrote: 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.
Are you sure that's how it's supposed to work? Or is this just some undefined behaviour that you're relying on? I'm not denying that it may very well work that way, but is that documented, or just a weird oddity of Windows? If it's the latter then your solution is not a very good one. Also why use "10" here:
CreateFileMapping(file,0,PAGE_WRITECOPY,0,10,0);
Again, your article is pretty sparse, and you don't have any comments, so it's really hard to know what's up.
¡El diablo está en mis pantalones! ¡Mire, mire!Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|