0

我正在尝试制作一个将生成 .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:歌曲与播放列表文件位于同一文件夹中。EDIT3:我正在使用 Windows 8 RTM 中包含的最新 Windows Media Player。

4

3 回答 3

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)."'/>");     
}
于 2012-08-28T09:04:55.150 回答
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>
于 2012-08-28T09:07:39.253 回答
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>
*/
于 2012-08-28T09:54:39.560 回答