douzuo5504 2012-08-28 08:59
浏览 111
已采纳

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

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.

  • 写回答

3条回答 默认 最新

  • drux41001 2012-08-28 09:54
    关注

    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>
    */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改