mediaelement.js - 播放随机视频?

发布于 2024-12-20 03:46:08 字数 300 浏览 1 评论 0原文

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

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

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

就太好了!

I have lots of videos in the directory "video" in my WordPress installation.

They all play nice using the MediaElement.js plugin, but is it also possible to play a random clip from this directory? For example using a shortcode that directs to the directory (and not to a specific video), something like

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

That would be great!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你げ笑在眉眼 2024-12-27 03:46:08

这应该是可能的。

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

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

例如

$defaultDirectory=site_url() +“/视频/”;

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 "";
}
}    

This should be possible.

What you might want to do is use AJAX to generate the div containing your video player. If you do this, you can delete/recreate the player very easily.

After that what you'd need is a shortcode definition which feeds a directory string value and a boolean value into whichever function you attach to the shortcode handler.

For Instance

$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
}

Also from 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 "";
}
}    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文