Search code examples
phpplaylistwindows-media-playerwpl

Create windows media player playlist with PHP


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.


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>
    */