我让卡片中的视频自动轮播,我需要知道某个视频何时结束?


好吧,我现在明白你想要什么了。在使用“[sound:1.mp4][sound:2.mp4]”格式的视频时,无法检测视频何时播放完毕。

您需要将视频以 javascript 数组格式列出,如[“1.mp4”, “2.mp4”]。

您必须将视频存储在两个字段中

  1. Videos = [sound:1.mp4][sound:2.mp4]
  2. Videos_Array = [“1.mp4”, “2.mp4”]
  • 确保 Videos_Array 包含有效的 javascript 数组

第一个字段是必填字段,否则 Anki 会自动删除您的视频文件:

Javascript 代码

那么,以下代码可能会以您想要的方式工作:

<video controls id="player" />

<script>
{
const wait = (t) => new Promise((s) => setTimeout(s, t));

try {
  videos = JSON.parse(`{{Videos_Array}}`);
} catch {
  console.log("Check syntax in videos array")
}

const player = document.getElementById("player");
let index = 0;

player.addEventListener("ended", async () => {
  if (++index < videos.length) {
    // wait 5 seconds before playing the next audio file
    await wait(5000);

    player.src = videos[index];
    // player.playbackRate = 1;
    player.play();
  } else {
    // If there are no more audio files, make it possible to play from
    // the first file again when the play button is clicked.
    index = 0;
    player.src = videos[index];
  }
});
// play the first audio file

player.src = videos[index];
// player.playbackRate = 1;
player.play();

// Replay all videos when pressing R
document.onkeyup = (e) => {
  if (e.key == "r") {
    player.src = videos[index];
    // player.playbackRate = 1;
    player.play();
  }
};

}
</script>

上述代码是根据这些示例修改的:

此方法的问题:

  • 如果在视频播放结束前前进到下一张卡,即使显示了下一张卡,播放仍将继续
  • 必须禁用 Anki 的 “自动前进 ”功能。如果要使用 “自动前进 ”功能,也必须使用 javascript 来处理。

Alright, I understand what you want now. It is not possible to detect when one video finishes playing while using videos in the "[sound:1.mp4][sound:2.mp4]” format.

You will need to instead have your videos listed in javascript array format like this [“1.mp4”, “2.mp4”].

You’ll have to store your videos in two fields

  1. Videos = [sound:1.mp4][sound:2.mp4]
  2. Videos_Array = [“1.mp4”, “2.mp4”]
  • Ensure that Videos_Array contains a valid javascript array

The first field is required because otherwise Anki will automatically delete your video files:

Javascript code

Then, the following code may work in the way you want:

<video controls id="player" />

<script>
{
const wait = (t) => new Promise((s) => setTimeout(s, t));

try {
  videos = JSON.parse(`{{Videos_Array}}`);
} catch {
  console.log("Check syntax in videos array")
}

const player = document.getElementById("player");
let index = 0;

player.addEventListener("ended", async () => {
  if (++index < videos.length) {
    // wait 5 seconds before playing the next audio file
    await wait(5000);

    player.src = videos[index];
    // player.playbackRate = 1;
    player.play();
  } else {
    // If there are no more audio files, make it possible to play from
    // the first file again when the play button is clicked.
    index = 0;
    player.src = videos[index];
  }
});
// play the first audio file

player.src = videos[index];
// player.playbackRate = 1;
player.play();

// Replay all videos when pressing R
document.onkeyup = (e) => {
  if (e.key == "r") {
    player.src = videos[index];
    // player.playbackRate = 1;
    player.play();
  }
};

}
</script>

The above code was modified from these examples:

Problems with this method:

  • If you advance to the next card before the video playback has finished, the playback will continue even as the next card is displayed
  • Anki’sv"Auto Advance" feature must be disabled. If you wish to use “Auto advance” it too must be handled using javascript instead.
1 Like