最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTMLMediaElement: The provided double value is non-finite - Stack Overflow

programmeradmin4浏览0评论

Hello im trying to set a volume to a video!

When i try to set the volume like this:

myVideoElement.volume = 0.801; // Is working fine!

But when i try to set the volume like this:

var volumeToSet = Math.floor(myPrcent * 10) / 1000;

myVideoElement.volume = volumeToSet;

I get this error:

How can fix that?

Hello im trying to set a volume to a video!

When i try to set the volume like this:

myVideoElement.volume = 0.801; // Is working fine!

But when i try to set the volume like this:

var volumeToSet = Math.floor(myPrcent * 10) / 1000;

myVideoElement.volume = volumeToSet;

I get this error:

How can fix that?

Share Improve this question edited Jan 24, 2018 at 18:15 John Stamoutsos asked Jan 24, 2018 at 18:07 John StamoutsosJohn Stamoutsos 3731 gold badge3 silver badges17 bronze badges 8
  • 1 I think the error is pretty self-explanitory. You must set an actual value, not a calculated one. – Scott Marcus Commented Jan 24, 2018 at 18:09
  • You think right! But is this the real problem? – John Stamoutsos Commented Jan 24, 2018 at 18:12
  • Can't say. You haven't explained what you are trying to do. – Scott Marcus Commented Jan 24, 2018 at 18:25
  • Im trying to set a volume to my video with my var! myVideoElement.volume = volumeToSet; lol – John Stamoutsos Commented Jan 24, 2018 at 18:42
  • 1 @ScottMarcus Of course it can be a calculated value. Your comment is nonsense. – Brad Commented Aug 7, 2023 at 4:46
 |  Show 3 more comments

5 Answers 5

Reset to default 7

Ok solved! I trying before to find the percentage of the video volume with:

var percent = Math.floor((left / Math.floor(myVideoElement.volume * 10) / 10) * 100);

The Math.floor() function makes my number infinity!

I've received similar error when I was using IntersectionObserver. It turned out that the entries according to the documentation have to be less than 1. Somehow I put 2.5345453453 as an entry and got this error. I hope it'll help someone as well.

The actual error could be better solved by checking if the value is finite or non-finite. A suggestion is to use:

isNaN(value) || !isFinite(value)

If the above is true, you could either use a default finite value, or cancel the action altogether.

For example:

const value = Math.floor(myPrcent * 10) / 1000;
if (isNaN(value) || !isFinite(value)) return defaultValue;
return value;

I had this issue with HTML5 videos. I was creating a preview system of a selected video, and wanted to grab a couple of different time slots for thumbnails. (Kind of like Youtube). I was getting this same error. Basically, I wanted each thumbnail/preview to be every 25% of the time of the video up to a certain point and I was running this calculation in a helper function.

I noticed from other answers that it cannot be a 'calculated value', however my issue was that I had the calculation run in an asynchronous function; this was returning a promise, rather than a raw integer, and causing the error.

This was a mistake on my part, I do a lot of projects in Node.js and writing the async keyword was just muscle memory. Removed it and worked perfectly.

const thumbnailTime = calcPercent(25, video.duration);
video.currentTime = thumbnailTime;

function calcPercent(percent, number) {
    return Math.floor(percent / 100 * number);
}

So if anyone has a scenario where they're doing any sort of calculation for audio or video in an asynchronous function, and you're coming across this error, this is probably why.

tips, store the duration float value within your database. and through your chrome detector function apply if chrome true write the duration value as a none changeable value to the media object. i discovered this do the missing 'Content-Length' header due file compression. tested works nicely with audio and video elements.

发布评论

评论列表(0)

  1. 暂无评论