刷新页面时播放随机新视频

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

playing random new video when refreshing page

问题

我正在尝试创建一个只显示一个视频的网站(因为它应该在手机上打开),但是当刷新页面时,会出现一个随机的新视频。我有8个MP4视频(带有Dropbox链接),我想在刷新页面时让视频在这8个视频之间随机播放。希望这样说得清楚。

以下是我目前已经做的事情 - 不幸的是它还没有生效

我认为这可能与我使用的URL有关。

HTML:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
    <script src="function.js"></script>
<title>未命名页面</title>
</head>
<body>
    <video id="videoItem">
    <source src="https://www.dropbox.com/s/s7nfpv6g65gjumc/film%202.mp4?raw=1" type="video/mp4" />
 
</video>
</body>
</html>

JavaScript:

// JavaScript Document
document.getElementById(videopage)
<script> 
function getRandomInt(min, max) {         
return Math.floor(Math.random() * array.length);
 	target = document.getElementById('videoItem');
     var sources = video.getElementsByTagName('source');
     var videoUrlsMp4=["https://www.dropbox.com/s/s7nfpv6g65gjumc/film%202.mp4?raw=1", "https://www.dropbox.com/s/qnmb9s5b0vho328/film%203.mp4?raw=1", "https://www.dropbox.com/s/ey4a07703h8henq/film%205.mp4?raw=1", "https://www.dropbox.com/s/et157quhjel6mcu/film%206.mp4?raw=1", "https://www.dropbox.com/s/e0nrb47fetotcsw/film%207.mp4?raw=1", "https://www.dropbox.com/s/npj70zhxzecs4lp/film%208.mp4?raw=1"];
     var random_nr = getRandomInt(0, videoUrlsMp4.length-1);
     sources[0].src = videoUrlsMp4[random_nr ];
     video.load(); </script>

请注意,上述JavaScript代码存在一些错误,需要修复以使其正常工作。

英文:

I am trying to make a website with only one video displayed (covering the whole page because it's supposed to be opened with a phone). But when refreshing the page a random new video appears. I have 8 mp4 videos (with a dropbox link) and Im trying to get the video to randomise between these 8 videos when refreshing the page. I hope this makes sense.

Here is what I have done so far - it's not working unfortunately

I think it might have to do with the url I am using.

HTML:

&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
	&lt;script src=&quot;function.js&quot;&gt;&lt;/script&gt;
&lt;title&gt;untitled page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;video id=&quot;videoItem&quot;&gt;
    &lt;source src=&quot;https://www.dropbox.com/s/s7nfpv6g65gjumc/film%202.mp4?raw=1&quot; type=&quot;video/mp4&quot; /&gt;
 
&lt;/video&gt;
&lt;/body&gt;
&lt;/html&gt;

JavaScript:

// JavaScript Document
document.getElementById(videopage)
&lt;script&gt; 
function getRandomInt(min, max) {         
return Math.floor(Math.random() * array.length);
 	target = document.getElementById(&#39;videoItem&#39;);
     var sources = video.getElementsByTagName(&#39;source&#39;);
     var videoUrl=[&quot;https://www.dropbox.com/s/s7nfpv6g65gjumc/film%202.mp4?raw=1&quot;, &quot;https://www.dropbox.com/s/qnmb9s5b0vho328/film%203.mp4?raw=1&quot;, &quot;https://www.dropbox.com/s/ey4a07703h8henq/film%205.mp4?raw=1&quot;, &quot;https://www.dropbox.com/s/et157quhjel6mcu/film%206.mp4?raw=1&quot;, &quot;https://www.dropbox.com/s/e0nrb47fetotcsw/film%207.mp4?raw=1&quot;, &quot;https://www.dropbox.com/s/npj70zhxzecs4lp/film%208.mp4?raw=1&quot;];
     var random_nr = getRandomInt(0, videoUrlsMp4.length-1);
     sources[0].src = videoUrlsMp4[random_nr ];
     video.load(); &lt;/script&gt;

答案1

得分: 1

以下是一个在每次页面加载时加载新视频的最小实现。

请注意,您不需要一个&lt;source&gt;元素,相反,显示的解决方案会将src属性添加到&lt;video&gt;元素本身。

还请注意,我已将mutedplaysinline属性添加到&lt;video&gt;元素,否则移动客户端可能会拒绝播放视频,因为它的加载没有由用户手势触发。

var videoEl = document.querySelector('#videoItem');

var videoUrls = [
  "https://www.dropbox.com/s/s7nfpv6g65gjumc/film%202.mp4?raw=1", 
  "https://www.dropbox.com/s/qnmb9s5b0vho328/film%203.mp4?raw=1", 
  "https://www.dropbox.com/s/ey4a07703h8henq/film%205.mp4?raw=1", 
  "https://www.dropbox.com/s/et157quhjel6mcu/film%206.mp4?raw=1", 
  "https://www.dropbox.com/s/e0nrb47fetotcsw/film%207.mp4?raw=1", 
  "https://www.dropbox.com/s/npj70zhxzecs4lp/film%208.mp4?raw=1"
];

videoEl.addEventListener('canplay', function () {
  videoEl.play();
});

videoEl.src = videoUrls[Math.floor(Math.random() * videoUrls.length)];
<video id="videoItem" src="https://www.dropbox.com/s/s7nfpv6g65gjumc/film%202.mp4?raw=1" muted playsinline>
</video>
英文:

Below is a minimal implementation that loads a new video on every page load.

Note that you do not need a &lt;source&gt;-element, instead the shown solution adds a src-attribute to the &lt;video&gt;-element itself.

Also note that I have added the muted and playsinline attributes to the &lt;video&gt;-element, otherwise mobile clients may/will refuse to play the video since loading it wasn't triggered by a user gesture.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var videoEl = document.querySelector(&#39;#videoItem&#39;);

var videoUrls = [
  &quot;https://www.dropbox.com/s/s7nfpv6g65gjumc/film%202.mp4?raw=1&quot;, 
  &quot;https://www.dropbox.com/s/qnmb9s5b0vho328/film%203.mp4?raw=1&quot;, 
  &quot;https://www.dropbox.com/s/ey4a07703h8henq/film%205.mp4?raw=1&quot;, 
  &quot;https://www.dropbox.com/s/et157quhjel6mcu/film%206.mp4?raw=1&quot;, 
  &quot;https://www.dropbox.com/s/e0nrb47fetotcsw/film%207.mp4?raw=1&quot;, 
  &quot;https://www.dropbox.com/s/npj70zhxzecs4lp/film%208.mp4?raw=1&quot;
];


videoEl.addEventListener(&#39;canplay&#39;, function () {
  videoEl.play();
});

videoEl.src = videoUrls[Math.floor(Math.random() * videoUrls.length)];

<!-- language: lang-html -->

&lt;video id=&quot;videoItem&quot; src=&quot;https://www.dropbox.com/s/s7nfpv6g65gjumc/film%202.mp4?raw=1&quot; muted playsinline&gt;
&lt;/video&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月6日 19:59:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628599.html
匿名

发表评论

匿名网友

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

确定