Question

I'm trying to make a script that will generate a .WPL file. The script scans the folder for all .mp3 files, and includes them in the .wpl file. However it doesn't seem to work, as Windows media player gives me an error that the file is corrupted.

What is wrong with the code? :)

    $ourFileName = "Playlist.wpl";
    $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
    echo "Created the playlist <br />";
    $firsthalf = "
    <?wpl version='1.0'?>
        <smil>
            <head>
                <meta name='Generator' content='Microsoft Windows Media Player -- 12.0.9200.16384'/>
                <meta name='IsNetworkFeed' content='0'/>
                <title>Playlist</title>
            </head>
        <body>
            <seq>";
    $secondhalf = "
        </seq>
        </body>
    </smil>
    ";

    fwrite($ourFileHandle, $firsthalf);

    foreach (glob("*.mp3") as $filename) {
        fwrite($ourFileHandle, "<media src='".$filename."'/>");     
    }       

    fwrite($ourFileHandle, $secondhalf);
    fclose($ourFileHandle);

EDIT: The generated .wpl file looks like this:

    <?wpl version='1.0'?>
        <smil>
            <head>
                <meta name='Generator' content='Microsoft Windows Media Player -- 12.0.9200.16384'/>
                <meta name='IsNetworkFeed' content='0'/>
                <title>Playlist</title>
            </head>
        <body>
            <seq><media src='FIRST SONG.mp3'/><media src='SECOND SONG.mp3'/>
        </seq>
        </body>
    </smil>

EDIT2: The songs are in the same folder as the playlist file. EDIT3: I'm using the newest Windows Media Player which is included in windows 8 RTM.

Was it helpful?

Solution

Why not use PHP to create the wpl file dynamically, I quickly put together this function, perhaps its of some interest, output into a file, browser or force download/send the file to the user.

<?php
create_playlist('./', "Playlist.wpl",'save');

/**
 * Using SimpleXMLElement create wmp playlist
 *
 * @param string $path_to_files - Pathe to mp3 files
 * @param string $save_path - path to save your xml
 * @param string $handle - download || save 
 */
function create_playlist($path_to_files, $save_path=null, $handle='download'){

    $xml = new SimpleXMLElement('<?wpl version="1.0"?><smil/>');
    $node = $xml->addChild('head');

    $meta = $node->addChild('meta', '');
    $meta->addAttribute('name', 'Generator');
    $meta->addAttribute('content', 'Microsoft Windows Media Player -- 12.0.9200.16384');

    $meta = $node->addChild('meta', '');
    $meta->addAttribute('name', 'IsNetworkFeed');
    $meta->addAttribute('content', '0');

    $node->addChild('title', 'Playlist');

    $body = $xml->addChild('body');
    $seq = $body->addChild('seq');

    foreach (glob($path_to_files."*.mp3") as $filename) {
        $media = $seq->addChild('media', "");
        $media->addAttribute('src', realpath($filename));
    }

    ob_start();
    echo $xml->asXML();
    $return = ob_get_contents();
    ob_end_clean();
    $return = trim(str_replace(array('<?xml version="1.0"?>','></media>','></meta>'),array('','/>','/>'),$return));

    if($handle == 'download'){
        //Force a download
        header('Content-Description: File Transfer');
        header('Content-Type: application/vnd.ms-wpl');
        header('Content-Disposition: attachment; filename=our_playlist.wpl');
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: ' . sprintf("%u", strlen($return)));
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Expires: 0');
        header('Pragma: public');
        exit($output);
    }elseif($handle == 'save'){
        file_put_contents($save_path, $return);
        return true;
    }else{
        exit($return);
    }
}

/**
 * Result
 * 
<?wpl version="1.0"?>
<smil>
   <head>
    <meta name="Generator" content="Microsoft Windows Media Player -- 12.0.9200.16384"/>
    <meta name="IsNetworkFeed" content="0"/>
    <title>Playlist</title>
   </head>

   <body>
    <seq>
        <media src="C:\xampp\htdocs\test.mp3"/>
        ...
        ...
    </seq>
   </body>
</smil>
*/

OTHER TIPS

The media src has to be fullpath to that song or at least relative to to .wpl file.

<seq><media src='c:\music\FIRST SONG.mp3'/><media src='c:\music\SECOND SONG.mp3'/></seq>

so you need:

foreach (glob("*.mp3") as $filename) {
    fwrite($ourFileHandle, "<media src='".realpath($filename)."'/>");     
}

My gut reaction is you needed an item count. I made a quick playlist the most noticable thing missing was your item count. As well as items on a different line although I would hope that was a lesser thing.

<?wpl version="1.0"?>
<smil>
    <head>
        <meta name="Generator" content="Microsoft Windows Media Player -- 12.0.7601.17514"/>
        <meta name="ItemCount" content="2"/>
        <title>test</title>
    </head>
    <body>
        <seq>
            <media src="Music\Faves\Dario G - Sunchyme [radio version].mp3" tid="{4B0B7EAC-98F9-4566-9A8C-80E92334D03A}"/>
            <media src="Music\Faves\Dario G - Sunchyme [original].mp3"/>
        </seq>
    </body>
</smil>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top