【问题标题】:mediaelement.js - Play random video?mediaelement.js - 播放随机视频?
【发布时间】:2011-12-28 19:07:13
【问题描述】:

我的 WordPress 安装目录“video”中有很多视频。

它们都使用MediaElement.js 插件播放得很好,但是是否也可以播放此目录中的随机剪辑?例如使用指向目录(而不是特定视频)的短代码,例如

[video src="http://www.domain.com/wordpress/wp-content/video" random="true"]

那太好了!

【问题讨论】:

    标签: wordpress mediaelement.js shortcode


    【解决方案1】:

    这应该是可能的。

    您可能想要做的是使用 AJAX 生成包含您的视频播放器的 div。如果你这样做,你可以很容易地删除/重新创建播放器。

    之后,您需要一个简码定义,它将目录字符串值和布尔值提供给您附加到简码处理程序的任何函数。

    举例

    $defaultDirectory=site_url()+"/videos/";

    add_shortcode( 'video', 'videoDiv' );
    
    function videoDiv( $shortcodeAttributeList )
    {
      extract( shortcode_atts( array(
                 'src'    => $defaultDirectory,
                 'random' => true,              /*set default values, use lowercase*/
             ), $shortcodeAttributeList ) );
    
      if($random)
      {
          $numFiles=fileCounter($src);
          $choice=rand(1, $numFiles);
      }
    
      $output='<div id="videoPlayer" class="player">';
      // Continue by making a string which spans from <div> to </div>
    
      return $output; //a div
    }
    

    同样来自http://php.net/manual/en/function.readdir.php

    <?php
    /**
    * get the number of files within a given dir
    */
    function fileCounter($dir){
        $counter = 0;
        if ($handle = opendir($dir)) {
          //echo "Directory handle: $handle\n";
          //echo "Files:\n";
          /* This is the correct way to loop over the directory. */
          while (false !== ($file = readdir($handle))) {
              //echo "<BR>".$counter." - $file";
              $counter++;
          }
          closedir($handle);
        }
        $counter -= 1; // in order to exclude '.' and '..', as well as start the counter on 1
        return $counter;
    }
    /**
    * get the filename in a giver dir, starting the first file with the index 1
    */
    function fileNameByIndex($dir, $fileIndex){
        $counter = 0;
        if ($handle = opendir($dir)) {
          while (false !== ($file = readdir($handle))) {
              $counter++;
              if($counter - 2 == $fileIndex)
                  return $file;
          }
          closedir($handle);
        }
        return "";
    }
    }    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多