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 に答える