Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP创建windows media player播放列表_Php_Playlist_Windows Media Player_Wpl - Fatal编程技术网

使用PHP创建windows media player播放列表

使用PHP创建windows media player播放列表,php,playlist,windows-media-player,wpl,Php,Playlist,Windows Media Player,Wpl,我正在尝试创建一个脚本,该脚本将生成一个.WPL文件。脚本扫描文件夹中的所有.mp3文件,并将它们包含在.wpl文件中。但是,它似乎不起作用,因为Windows media player给我一个错误,文件已损坏 代码有什么问题?:) $ourFileName=“Playlist.wpl”; $ourFileHandle=fopen($ourFileName,'w')或die(“无法打开文件”); echo“创建播放列表”; $firsthalf=” 播放列表 "; $secondhalf=” "

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

代码有什么问题?:)

$ourFileName=“Playlist.wpl”;
$ourFileHandle=fopen($ourFileName,'w')或die(“无法打开文件”);
echo“创建播放列表
”; $firsthalf=” 播放列表 "; $secondhalf=” "; fwrite($ourFileHandle,$firsthalf); foreach(glob(“*.mp3”)作为$filename){ fwrite($ourFileHandle,“”); } 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。

媒体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)."'/>");     
}
foreach(glob(“*.mp3”)作为$filename){
fwrite($ourFileHandle,“”);
}

媒体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)."'/>");     
}
foreach(glob(“*.mp3”)作为$filename){
fwrite($ourFileHandle,“”);
}

我的直觉反应是你需要物品计数。我做了一个快速播放列表,最值得注意的是你的物品数量。以及不同行上的项目,尽管我希望这是一件次要的事情

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

测试

我的直觉反应是你需要物品计数。我做了一个快速播放列表,最值得注意的是你的物品数量。以及不同行上的项目,尽管我希望这是一件次要的事情

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

测试

为什么不使用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>
*/

为什么不使用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>
*/


你能发布玩家列表的示例输出吗?编辑了帖子并发布了输出:)你能发布玩家列表的示例输出吗?编辑了帖子并发布了输出:)
ItemCount
和换行符不是必需的。问题似乎是我没有将文件解析为XML文件,但是普通的…
ItemCount
和换行符是不需要的。问题似乎是我没有将文件解析为XML文件,而是普通的…从我自己的简单实验来看,如果我将播放列表放在音乐中,我不需要文件的相对路径,而是从我的用户配置文件。两者都可以。完整路径最好。歌曲与播放列表文件位于同一文件夹中。而且,它仍然不起作用。谢谢你的解决方案!然后是您的脚本问题,您的媒体播放器有问题。我正在使用Windows 8 RTM中包含的最新Windows Media Player。根据我自己的简单实验,如果我将播放列表放入音乐中,我的播放列表不需要来自文件的相对路径,而是来自我的用户配置文件。两者都可以。完整路径最好。歌曲与播放列表文件位于同一文件夹中。而且,它仍然不起作用。谢谢你的解决方案!然后是您的脚本问题,您的媒体播放器有问题。我正在使用Windows 8 RTM中包含的最新Windows Media Player。非常感谢!似乎问题在于我没有将该文件解析为XML文件,而是普通文件…非常感谢!似乎问题在于我不是将文件解析为XML文件,而是普通文件。。。