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

似乎anki 提供了某个视频播放结束的回调通知,我不知道如何使用?

我的卡片有多个视频,我让它们自动循环播放,为了精确控制,我需要每播放完一个视频,anki 都能对卡片发送通知(或者卡片都能监听到某个事件),我根据通知或监听到的事件进入下一个视频的播放。我现在都是预估视频的时长的,这个很不准。

感谢!

1 Like

你好,我在用人工智能翻译回答你的问题。

听起来您是用 javascript 来控制视频播放的,是吗?

您是这样做的吗?
my_video_1 = my_video1.webm
my_video_2 = my_video2.webm
my_video_3 = my_video3.webm

<video src={{my_video_1}} />
<video src={{my_video_2}} />
<video src={{my_video_3}} />

<script>
document.querySelectorAll("video").forEach( ...
  ...
</script>

ANKI 可以自动依次播放声音和视频文件,但要求视频文件为 [sound:my_video.webm] 格式。因此,请改成这样

my_video_1 = [sound:my_video1.webm]
my_video_2 = [sound:my_video2.webm]
my_video_3 = [sound:my_video3.webm]

{{my_video_1}}
{{my_video_2}}
{{my_video_3}}

然后,要再次重复播放所有视频,只需按下 R


In English:

Hi, I’m answering your question using AI translation.

It sounds like you’re using javascript to control the playing your videos, is that so?

Are you doing this?
my_video_1 = my_video1.webm
my_video_2 = my_video2.webm
my_video_3 = my_video3.webm

<video src={{my_video_1}} />
<video src={{my_video_2}} />
<video src={{my_video_3}} />

<script>
document.querySelectorAll("video").forEach( ...
  ...
</script>

ANKI can automatically play both sound and video files in sequence but it requires that your video files are in the [sound:my_video.webm] format. So, instead like this:

my_video_1 = [sound:my_video1.webm]
my_video_2 = [sound:my_video2.webm]
my_video_3 = [sound:my_video3.webm]

{{my_video_1}}
{{my_video_2}}
{{my_video_3}}

Then, to repeat playing all the videos again you’d simply press R

1 Like

我放到字段中的音频文件,比如"1.mp4;2.mp4",它会自动在字段中形成这样的格式: “[sound:1.mp4][sound:2.mp4]”。

anki 并不允许我们直接调用这个音频文件,我通过 anki 处理后返回给我的页面文件的代码得知,这两个音频文件被处理为超链接,元素class为 soundLink;anki 返回的页面代码如下:

<a class="replay-button soundLink" href="#" onclick="pycmd('play:q:0');
return false;">
<svg class="playImage" viewBox="0 0 64 64" version="1.1">
<circle cx="32" cy="32" r="29"></circle>
<path
d="M56.502,32.3011-37.502,20.10110.329,-40.804137.173,20.703Z"></
path>
</svg>
</a>
<a class="replay-button soundLink" href="#" onclick="pycmd('play:q:1');
return false;">
<svg class="playImage" viewBox="0 0 64 64" version="1.1">
<circle cx="32" cy="32" r="29"></circle>
<path
d="M56.502,32.3011-37.502,20.10110.329,-40.804137.173,20.703Z"></
path>
</svg>
</a>

通过控制以下代码的 index 变量,我可以实现播放任何一段音频:

var videoElement= document.getElementsByClassName("soundLink");
videoElement[index].click();

但现在的问题是,每个音频播放何时结束,anki 其实是可以给我们一个结束事件的,我在卡片中可以利用这个结束事件明确视频1播放结束了,我需要暂停5秒再播放视频2。

如果不知道视频何时结束,连播的过程中,这个暂停5秒就无法实现。

感谢你的热心回复。


好吧,我现在明白你想要什么了。在使用“[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

感谢!这确实是一种解决方案。利用video元素提供的视频结束事件。

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.