gpt4 book ai didi

javascript - 动态更改视频使 play() 请求被新的加载请求中断

转载 作者:数据小太阳 更新时间:2023-10-29 04:14:31 24 4
gpt4 key购买 nike

动态更改视频时,我在服务器控制台下收到以下错误

(index):71 Uncaught (in promise) DOMException: The play() request was interrupted by a new load request.

发生变化时我正在使用以下代码

function playlocalVID()
{
var player = document.getElementById("video");
var currentVID = document.getElementById('currentVID');
var selectedLocalVID = document.getElementById('howtovideo').files[0];
currentVID.setAttribute('src', URL.createObjectURL(new Blob([selectedLocalVID])));
player.load();
player.play();
}

但是当更改视频 3 - 4 次或点击删除按钮时

我遇到了问题

Uncaught (in promise) DOMException: The play() request was interrupted by a new load request.

这是我的 fiddle

https://jsfiddle.net/q3hhk17e/36/

你能告诉我如何解决这个问题吗。

最佳答案

附上canplay事件到 #video元素,在 playlocalVID() 内调用创建 Blob URL来自 File对象,请注意没有必要同时调用 Blob()File对象作为参数;设置电流 File.name#video .dataset ;设置 <input type="file">元素 .valuenull对于 change连续选择相同文件时调用的事件;调用.load()#video元素;等待canplaycanplaythrough将在 #video 分派(dispatch)的事件元素;在 canplay事件处理程序调用 .play()#video元素。

<!DOCTYPE html>
<html>

<head>
</head>

<body>
<div class="form-group last">
<label class="control-label col-md-3">How to video</label>
<div class="col-md-9">
<div class="fileinput fileinput-new" data-provides="fileinput">
<figure>
<video id="video" width="150" height="100" controls>
<source id='currentVID' src="" type="video/mp4">
</video>
<figcaption></figcaption>
</figure>
<div class="fileinput-preview fileinput-exists" style="max-width: 200px; max-height: 150px;"></div>
<div>
<span class="btn default btn-file">
<span class="fileinput-new"> Select Video </span>
<span class="fileinput-exists"> Change </span>
<input type="file" id="howtovideo" name="howtovideo" accept="video/mp4" autoplay onchange="playlocalVID();" />
</span>
<a href="javascript:;" class="btn red fileinput-exists removepic" data-name="removeforhowtovideo" data-dismiss="fileinput"> Remove </a>
</div>
</div>
</div>
</div>
<script>
var player = document.getElementById("video");
var currentVID = document.getElementById('currentVID');
var caption = document.querySelector("figcaption");
var selectedLocalVID = document.getElementById("howtovideo");
// reference to `Blob URL` to be created at `playLocalVID`
var url;
// attach `canplay` event to `player`
player.addEventListener("canplay", function handleEvent(e) {
this.play();
// set `figcaption` to current video `File.name`
caption.innerHTML = this.dataset.currentVideo;
});

function playlocalVID() {
if (url) {
console.log(url);
// if `url` is defined revoke existing `Blob URL`
URL.revokeObjectURL(url);
}
// create `Blob URL` of `File` object
url = URL.createObjectURL(selectedLocalVID.files[0]);
// set `.name` of current `File` at `player.dataset.currentVideo`
player.dataset.currentVideo = selectedLocalVID.files[0].name;
// reset `input type="file"` event
selectedLocalVID.value = null;
// call `.pause()` at `player`
player.pause();
// set new `src` at `<source>` element
currentVID.setAttribute("src", url);
// call `.load()` on `player`
// wait for `canplay` event
player.load();
}
</script>
</body>

</html>

关于javascript - 动态更改视频使 play() 请求被新的加载请求中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40234873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com