dongzun9958 2011-07-15 06:40
浏览 80
已采纳

在php中为jplayer生成播放列表

i am actually working on this mentioned title. player show the list, its generating perfectly. but i am no where to make it actually play that file. i must be wrong some where. i need advise fox. (ahh if i can attach the files.)

my class

    class DecodDir 
        {
            function getFiles($directory)
            {
                $all_files  = array();
                $handler    = opendir($directory);
                while($files=readdir($handler))
                {
                    if($files!="." && $files!="..")
                    {
                        $all_files[]= $files;
                    }
                }
                closedir($handler);
                return $all_files;
            }
        }

################# file where i am using this class *###############



 <?php

 include("decoddir.php");

 $obj       = new DecodDir();
 $results   = $obj->getFiles("mp3");
 $total     = count($results);
 $string        = "";
 for($i=0; $i<$total; $i++){

    $string .="
        {
        name:'$results[$i]',
        mp3:'mp3/$results[$i]',
        ogg:'$results[$i]'
        },
        ";
 }

    ?>
// its at the top of that html file (ofcorse with the php ext)

and below, this is where it is generating the playlist

var audioPlaylist = new Playlist("2", [
<?php echo $string; ?>
],

http://www.jplayer.org/latest/demo-02/ (the link from where i get jplayer) you can see the audio player with playlist. (actually i don't know hot format the code in here stackoverflow) thanks Rafay

  • 写回答

2条回答 默认 最新

  • duangu3620 2011-07-15 08:51
    关注

    I have taken the liberty of re-factoring the code a bit for you. I don't know exactly what you are trying to do, but it will help to have the beginnings of a better class on your side.

    <?php
    
    class DecodDir
    {
    
        private
            $directory,
            $files;
    
        public function __construct( $directory = null )
        {
            if ( ! is_null($directory) )
            {
                $this->setDirectory( $directory );
            }
        }
    
        public function setDirectory( $directory )
        {
            $this->directory = $directory;
            $this->files = null;
    
            // TODO put some validation in here;
    
            return $this;
        }
    
        public function getDirectory()
        {
            if ( is_null($this->directory) )
            {
                $this->directory = './';
            }
            return $this->directory;
        }
    
        private function getFiles()
        {
            if ( is_null($this->files) )
            {
                $this->files = array();
                $handler    = opendir( $this->getDirectory() );
                while($files=readdir($handler))
                {
                    if($files!="." && $files!="..")
                    {
                        $this->files[]= $files;
                    }
                }
                closedir($handler);
            }
    
            return $this->files;
        }
    
        public function getJson()
        {
            $list = array();
    
            foreach ( $this->getFiles() as $filename )
            {
                $item = new stdClass();
    
                $item->name = $filename;
                $item->mp3 = "mp3/{$filename}";
                $item->ogg = $filename;
    
                $list[] = $item;
            }
    
            $json = json_encode( $list );
    
            return $json;
    
        }
    
        public function countFiles()
        {
            return sizeof( $this->getFiles() );
        }
    
    }
    
    
    $obj = new DecodDir( 'mp3' );
    echo $obj->getJson();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line