使用PHP创建Windows媒体播放器播放列表

时间:2012-08-28 08:59:23

标签: php playlist windows-media-player wpl

我正在尝试创建一个生成.WPL文件的脚本。该脚本扫描文件夹中的所有.mp3文件,并将它们包含在.wpl文件中。但它似乎不起作用,因为Windows媒体播放器给我一个文件已损坏的错误。

代码有什么问题? :)

    $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);

编辑:生成的.wpl文件如下所示:

    <?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:歌曲与播放列表文件位于同一文件夹中。 编辑3:我正在使用Windows 8 RTM中包含的最新Windows Media Player。

3 个答案:

答案 0 :(得分:2)

media src必须是该歌曲的完整路径,或至少相对于.wpl文件。

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

所以你需要:

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

答案 1 :(得分:1)

我的直觉反应是你需要一个项目计数。我做了一个快速的播放列表,最值得注意的是你的项目数。以及不同行上的项目,虽然我希望这是一个较小的东西。

<?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>

答案 2 :(得分:1)

为什么不使用PHP动态创建wpl文件,我很快将这个函数放在一起,也许是它的一些兴趣,输出到文件,浏览器或强制下载/发送文件给用户。

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