Video Player Local Storage

huangapple go评论72阅读模式
英文:

Video Player Local Storage

问题

我想要将视频的durationsrc存储到本地存储中,但这段代码不起作用。视频的srcduration在检查元素应用程序本地存储中显示,但对于视频播放器不起作用。

window.addEventListener("unload", () => {
  let setDuration = localStorage.setItem("duration", `${mainVideo.currentTime}`);
  let setSrc = localStorage.setItem("src", `${mainVideo.getAttribute("src")}`);
});

window.addEventListener("load", () => {
  let getDuration = localStorage.getItem("duration");
  let getSrc = localStorage.getItem("src");
  if (getSrc) {
    mainVideo.src = getSrc;
    mainVideo.currentTime = getDuration;
  }
});

请注意,这段代码应该放在适当的上下文中,确保mainVideo已正确定义。

英文:

I want to store video duration and ``src to local storage, but this code not working. The video src and duration showing in inspect element application local storage but not working for the video player.

window.addEventListener("unload", () => {
  let setDuration = localStorage.setItem(
    "duration",
    `${mainVideo.currentTime}`
  );
  let setSrc = localStorage.setItem("src", `${mainVideo.getAttribute("src")}`);
});

window.addEventListener("load", () => {
  let getDuration = localStorage.getItem("duration");
  let getSrc = localStorage.getItem("src").src;
  if (getSrc) {
    mainVideo.src = getSrc;
    mainVideo.currentTime = getDuration;
  }
});

答案1

得分: 2

一个问题肯定就在这里

localStorage.getItem("src").src

您试图读取属性src,但localStorage.getItem("src")返回的是一个字符串null而不是一个对象。删除.src部分,该错误将得到解决。

如果这不是您的问题,我很抱歉。如果是的话,请提供有关您的问题或任何错误消息的更多信息。

英文:

One problem certainly lays right here

localStorage.getItem("src").src

You are trying to read the property src but localStorage.getItem("src") returns a string or null not an object. Remove the .src part and that error will be resolved.

I'm sorry if that wasn't your problem. If so please provide further information about your problem or any error messages.

huangapple
  • 本文由 发表于 2023年1月9日 02:24:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定