1

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

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

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

那很好啊!

4

1 回答 1

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 "";
}
}    
于 2011-12-21T08:44:53.060 回答