我目前正在与Tauri和Svelte一起创建一个Windows应用程序,但遇到了一些问题.我有一个按钮,当点击它打开文件系统,允许 Select 一个新的视频.当 Select 新视频时,我使用新路径更新视频存储.这一切都工作得很好,使用下面的代码,视频然后更新以显示新的视频,但是我也想重置time = 0,以便新的视频从头开始,但这在这里没有发生.因此,如果 Select 新文件时上一个视频的开始时间为20秒,则新视频的开始时间也为20秒.

当点击重置按钮时,时间变回0,但我希望它也自动发生时,视频store 得到更新和新的视频被加载.

$VideoStore发生变化时,我会更新time,时间与视频的currentTime有关:

    $: {
        time = 0;
        video_src = `https://asset.localhost/${$VideoStore}`;
    }

The full code:

Video.svelte

<script>
    import VideoStore from "../stores/VideoStore";

    let video_src = $VideoStore;

    let time = 0;
    let duration;
    let paused = true;

    const skip = () => {
        time += 10;
    };

    function resetTime() {
        time = 0;
    }

    $: {
        time = 0;
        video_src = `https://asset.localhost/${$VideoStore}`;
    }
</script>

<p>{time}</p>
<button on:click={resetTime}>Reset</button>
<button on:click={skip}>Skip 10 seconds</button>
<!-- svelte-ignore a11y-media-has-caption -->
<div class="player">
    {#key video_src}
        <video
            src={video_src}
            width="1280"
            height="720"
            bind:currentTime={time}
            bind:duration
            bind:paused
            controls
        >
        </video>
    {/key}
</div>

OpenButton.svelte

<script>
    import { open } from "@tauri-apps/api/dialog";
    import { documentDir } from "@tauri-apps/api/path";

    import VideoStore from "../stores/VideoStore";

    let filename = "";

    const openfile = async () => {
        const selected = await open({
            directory: false,
            multiple: false,
            defaultPath: await documentDir(),
        });

        filename = selected.replace(/\\/g, "/");
        VideoStore.update(() => {
            return filename;
        });
    };
</script>

<button on:click={openfile}>Open File</button>

推荐答案

#key可能会导致video的新实例使用旧的时间值进行初始化.如果在没有重新创建元素的情况下切换src,时间将自动重置为0.

REPL

Javascript相关问答推荐

鼠标移动时更新画布

仅在React和JS中生成深色

如何将拖放功能添加到我已自定义为图像的文件输入HTML标签中?

如何修复循环HTML元素附加函数中的问题?

仅圆角的甜甜圈图表

角色 map 集/spritebook动画,用户输入不停止在键上相位器3

我应该绑定不影响状态的函数吗?'

CheckBox作为Vue3中的一个组件

如何将Openjphjs与next.js一起使用?

TypeError:无法分解';React2.useContext(...)';的属性';basename';,因为它为空

如何使用TypeScrip设置唯一属性?

MarkLogic-earch.suggest不返回任何值

与svg相反;S getPointAtLength(D)-我想要getLengthAtPoint(x,y)

使用jQuery find()获取元素的属性

为列表中的项目设置动画

如何找到带有特定文本和测试ID的div?

Reaction useState和useLoaderData的组合使用引发无限循环错误

我不知道如何纠正这一点.

未捕获的不变违规:即使在使用DndProvider之后也应使用拖放上下文

ComponentWillReceiveProps仍在React 18.2.0中工作