html 使用video.play()时,视频显示白色屏幕

uz75evzq  于 2024-01-04  发布在  其他
关注(0)|答案(3)|浏览(222)

我想在一个网站上播放一个视频文件,我有这个功能,通过它我试图做到这一点

  1. function closePageWithCountdown(seconds) {
  2. const countdownElement = document.createElement("countdowntime");
  3. countdownElement.style.position = "fixed";
  4. countdownElement.style.top = "0";
  5. countdownElement.style.left = "0";
  6. countdownElement.style.backgroundColor = "red";
  7. countdownElement.style.color = "white";
  8. countdownElement.style.padding = "5px";
  9. document.body.appendChild(countdownElement);
  10. function updateCountdown() {
  11. countdownElement.textContent = `Page will automatically close in ${seconds} seconds`;
  12. if (seconds === 0) {
  13. document.body.removeChild(countdownElement);
  14. document.getElementsByTagName('html')[0].remove();
  15. document.write('Resource Exhausted !\n<br>You may reload the page or close the tab.');
  16. var centeredText = document.createElement("div");
  17. centeredText.innerHTML = "Some Text Here";
  18. centeredText.style.position = "fixed";
  19. centeredText.style.bottom = "0";
  20. centeredText.style.left = "0";
  21. centeredText.style.right = "0";
  22. centeredText.style.textAlign = "center";
  23. // centeredText.style.backgroundColor = "rgba(255, 255, 255, 0.8";
  24. centeredText.style.padding = "10px";
  25. centeredText.style.zIndex = "999"; // Ensures it appears on top of other content
  26. // Append the element to the body
  27. document.body.appendChild(centeredText);
  28. document.getElementsByTagName('html')[0].style.overflow = 'hidden';
  29. document.getElementsByTagName('body')[0].style.overflow = 'hidden';
  30. var colors = ['red', 'blue', 'green']; // List of rainbow colors
  31. var colorIndex = 0; // Initialize color index
  32. function flash() {
  33. centeredText.style.color = colors[colorIndex];
  34. colorIndex = (colorIndex + 1) % colors.length; // Cycle through colors
  35. }
  36. var clr = setInterval(flash, 500);
  37. // Create a new div element to hold the text
  38. var flashbackText = document.createElement('div');
  39. flashbackText.innerHTML = 'Click here to watch';
  40. flashbackText.style.position = 'absolute';
  41. flashbackText.style.top = '50%';
  42. flashbackText.style.left = '50%';
  43. flashbackText.style.transform = 'translate(-50%, -50%)';
  44. flashbackText.style.fontSize = '1.5em';
  45. flashbackText.style.fontWeight = 'bold';
  46. flashbackText.style.backgroundColor = 'yellow';
  47. flashbackText.style.padding = '10px';
  48. flashbackText.style.cursor = 'pointer';
  49. flashbackText.style.zIndex = '1000'; // Ensure it's above other elements
  50. // Append the text to the body
  51. document.body.appendChild(flashbackText);
  52. // Event listener for the click event
  53. flashbackText.addEventListener('click', function() {
  54. // Clear the screen
  55. document.body.innerHTML = '';
  56. // Create a video element
  57. var video = document.createElement('video');
  58. video.id = 'myVideo';
  59. video.style.position = 'absolute';
  60. video.style.top = '50%';
  61. video.style.left = '50%';
  62. video.style.transform = 'translate(-50%, -50%) rotate(90deg)'; // Rotate the video 90 degrees
  63. video.style.width = '100vh'; // Use viewport height as width for landscape
  64. video.style.height = '100vw'; // Use viewport width as height for landscape
  65. video.style.objectFit = 'contain'; // Ensure it covers the full area
  66. video.autoplay = true;
  67. video.controls = false; // Hide controls
  68. // Set the source of the video
  69. var source = document.createElement('source');
  70. source.src = './myvideo.mp4'; // Replace with the path to your video
  71. source.type = 'video/mp4';
  72. // Append source to video element
  73. video.appendChild(source);
  74. // Add the video to the screen
  75. document.body.appendChild(video);
  76. video.onloadedmetadata = function() {
  77. console.log('Video metadata loaded successfully');
  78. console.log('Video duration:', video.duration);
  79. };
  80. video.onerror = function() {
  81. console.error('Error loading the video');
  82. };
  83. // Play the video
  84. video.play();
  85. // Event listener for when the video ends
  86. video.addEventListener('ended', function() {
  87. // Clear the video and show the original text again
  88. document.body.innerHTML = '';
  89. document.body.appendChild(flashbackText);
  90. document.body.appendChild(centeredText);
  91. document.write('Resource Exhausted !\n<br>You may reload the page or close the tab.');
  92. });
  93. });
  94. } else {
  95. seconds--;
  96. setTimeout(updateCountdown, 1000);
  97. }
  98. }
  99. updateCountdown();
  100. }
  101. closePageWithCountdown(1) // 1 sec for testing

字符串

预期行为:有一个突出显示的文本(在屏幕中心),如果用户点击它,它会播放视频,当视频结束时,它会返回到页面的旧内容。

然而,在网站上它显示一个白色屏幕,没有声音。
我不知道我在这里做错了什么,是不是我没有在我的index.html文件中包含任何<video>标记?
此外,我没有获得video.onloadedmetadatavideo.onerror的任何控制台日志
(Note:当我使用telegra.ph视频链接作为源时,它有时会工作。

tyg4sfes

tyg4sfes1#

source是一个undefined变量,试图引用它会导致脚本崩溃。你需要设置video的属性,当然你只需要将video添加到HTML中。在计数器的末尾,你会显示资源已经耗尽,这是不正确的,因为资源工作正常。您不需要删除HTML的第一个子元素,也不需要在计数器完成其工作时添加该文本。您只需要在视频结束时执行此操作。请参阅下面的片段:

  1. function closePageWithCountdown(seconds) {
  2. const countdownElement = document.createElement("countdowntime");
  3. countdownElement.style.position = "fixed";
  4. countdownElement.style.top = "0";
  5. countdownElement.style.left = "0";
  6. countdownElement.style.backgroundColor = "red";
  7. countdownElement.style.color = "white";
  8. countdownElement.style.padding = "5px";
  9. document.body.appendChild(countdownElement);
  10. function updateCountdown() {
  11. countdownElement.textContent = `Page will automatically close in ${seconds} seconds`;
  12. if (seconds === 0) {
  13. document.body.removeChild(countdownElement);
  14. //document.getElementsByTagName('html')[0].remove();
  15. //document.write('Resource Exhaustedd !\n<br>You may reload the page or close the tab.');
  16. var centeredText = document.createElement("div");
  17. centeredText.innerHTML = "Some Text Here";
  18. centeredText.style.position = "fixed";
  19. centeredText.style.bottom = "0";
  20. centeredText.style.left = "0";
  21. centeredText.style.right = "0";
  22. centeredText.style.textAlign = "center";
  23. // centeredText.style.backgroundColor = "rgba(255, 255, 255, 0.8";
  24. centeredText.style.padding = "10px";
  25. centeredText.style.zIndex = "999"; // Ensures it appears on top of other content
  26. // Append the element to the body
  27. document.body.appendChild(centeredText);
  28. document.getElementsByTagName('html')[0].style.overflow = 'hidden';
  29. document.getElementsByTagName('body')[0].style.overflow = 'hidden';
  30. var colors = ['red', 'blue', 'green']; // List of rainbow colors
  31. var colorIndex = 0; // Initialize color index
  32. function flash() {
  33. centeredText.style.color = colors[colorIndex];
  34. colorIndex = (colorIndex + 1) % colors.length; // Cycle through colors
  35. }
  36. var clr = setInterval(flash, 500);
  37. // Create a new div element to hold the text
  38. var flashbackText = document.createElement('div');
  39. flashbackText.innerHTML = 'Click here to watch';
  40. flashbackText.style.position = 'absolute';
  41. flashbackText.style.top = '50%';
  42. flashbackText.style.left = '50%';
  43. flashbackText.style.transform = 'translate(-50%, -50%)';
  44. flashbackText.style.fontSize = '1.5em';
  45. flashbackText.style.fontWeight = 'bold';
  46. flashbackText.style.backgroundColor = 'yellow';
  47. flashbackText.style.padding = '10px';
  48. flashbackText.style.cursor = 'pointer';
  49. flashbackText.style.zIndex = '1000'; // Ensure it's above other elements
  50. // Append the text to the body
  51. document.body.appendChild(flashbackText);
  52. // Event listener for the click event
  53. flashbackText.addEventListener('click', function() {
  54. // Clear the screen
  55. document.body.innerHTML = '';
  56. // Create a video element
  57. var video = document.createElement('video');
  58. video.id = 'myVideo';
  59. video.style.position = 'absolute';
  60. video.style.top = '50%';
  61. video.style.left = '50%';
  62. video.style.transform = 'translate(-50%, -50%) rotate(90deg)'; // Rotate the video 90 degrees
  63. video.style.width = '100vh'; // Use viewport height as width for landscape
  64. video.style.height = '100vw'; // Use viewport width as height for landscape
  65. video.style.objectFit = 'contain'; // Ensure it covers the full area
  66. video.autoplay = true;
  67. video.controls = false; // Hide controls
  68. // Set the source of the video
  69. //var source = document.createElement('source');
  70. //source.src = './myvideo.mp4'; // Replace with the path to your video
  71. video.src = 'https://telegra.ph/file/9d80fe1d60b2896d8b47b.mp4'; // Replace with the path to your video
  72. video.type = 'video/mp4';
  73. //source.type = 'video/mp4';
  74. // Append source to video element
  75. //document..appendChild(source);
  76. // Add the video to the screen
  77. document.body.appendChild(video);
  78. video.onloadedmetadata = function() {
  79. console.log('Video metadata loaded successfully');
  80. console.log('Video duration:', video.duration);
  81. };
  82. video.onerror = function() {
  83. console.error('Error loading the video');
  84. };
  85. // Play the video
  86. video.play();
  87. // Event listener for when the video ends
  88. video.addEventListener('ended', function() {
  89. // Clear the video and show the original text again
  90. document.body.innerHTML = '';
  91. document.body.appendChild(flashbackText);
  92. document.body.appendChild(centeredText);
  93. document.write('Resource Exhausted !\n<br>You may reload the page or close the tab.');
  94. });
  95. });
  96. } else {
  97. seconds--;
  98. setTimeout(updateCountdown, 1000);
  99. }
  100. }
  101. updateCountdown();
  102. }
  103. closePageWithCountdown(1) // 1 sec for testing

个字符

展开查看全部
kxeu7u2r

kxeu7u2r2#

当我获取您的代码并添加测试视频时,代码似乎正在执行您的意图。
由于结果会弹出另一个视频链接,我的假设是问题出在你引用的视频或你用来引用视频的文件路径上。

  1. var video = document.createElement('video');
  2. video.id = 'myVideo';
  3. video.style.position = 'absolute';
  4. video.style.top = '50%';
  5. video.style.left = '50%';
  6. video.style.transform = 'translate(-50%, -50%) rotate(90deg)'; // Rotate the video 90 degrees
  7. video.style.width = '100vh'; // Use viewport height as width for landscape
  8. video.style.height = '100vw'; // Use viewport width as height for landscape
  9. video.style.objectFit = 'contain'; // Ensure it covers the full area
  10. video.autoplay = true;
  11. video.controls = false; // Hide controls
  12. // Set the source of the video
  13. var source = document.createElement('source');
  14. source.src = 'https://samplelib.com/lib/preview/mp4/sample-5s.mp4'; // Replace with the path to your video
  15. source.type = 'video/mp4';
  16. // Append source to video element
  17. video.appendChild(source);
  18. // Add the video to the screen
  19. document.body.appendChild(video);
  20. video.onloadedmetadata = function() {
  21. console.log('Video metadata loaded successfully');
  22. console.log('Video duration:', video.duration);
  23. };
  24. video.onerror = function() {
  25. console.error('Error loading the video');
  26. };
  27. // Play the video
  28. video.play();

字符串

展开查看全部
py49o6xq

py49o6xq3#

当您将document.body.innerHTML设置为空字符串时,您实际上删除了所有现有元素,包括触发视频播放的脚本。这可能会导致意外行为。
与其清除整个document.body,不如考虑创建一个容器元素来同时保存视频和原始文本。然后,您可以将该容器的内容替换为视频,并在需要时恢复为原始文本。下面是代码的修改版本来说明这一点

  1. function closePageWithCountdown(seconds) {
  2. const countdownElement = document.createElement("countdowntime");
  3. countdownElement.style.position = "fixed";
  4. countdownElement.style.top = "0";
  5. countdownElement.style.left = "0";
  6. countdownElement.style.backgroundColor = "red";
  7. countdownElement.style.color = "white";
  8. countdownElement.style.padding = "5px";
  9. document.body.appendChild(countdownElement);
  10. function updateCountdown() {
  11. countdownElement.textContent = `Page will automatically close in ${seconds} seconds`;
  12. if (seconds === 0) {
  13. document.body.removeChild(countdownElement);
  14. // Create a container to hold the video and original text
  15. var container = document.createElement('div');
  16. container.style.position = 'absolute';
  17. container.style.top = '0';
  18. container.style.left = '0';
  19. container.style.width = '100%';
  20. container.style.height = '100%';
  21. container.style.overflow = 'hidden';
  22. document.body.appendChild(container);
  23. // Original text
  24. var centeredText = document.createElement("div");
  25. centeredText.innerHTML = "Some Text Here";
  26. centeredText.style.position = "fixed";
  27. centeredText.style.bottom = "0";
  28. centeredText.style.left = "0";
  29. centeredText.style.right = "0";
  30. centeredText.style.textAlign = "center";
  31. centeredText.style.padding = "10px";
  32. centeredText.style.zIndex = "999"; // Ensures it appears on top of other content
  33. container.appendChild(centeredText);
  34. // Video container
  35. var videoContainer = document.createElement('div');
  36. videoContainer.style.position = 'absolute';
  37. videoContainer.style.top = '0';
  38. videoContainer.style.left = '0';
  39. videoContainer.style.width = '100%';
  40. videoContainer.style.height = '100%';
  41. container.appendChild(videoContainer);
  42. // Event listener for the click event
  43. centeredText.addEventListener('click', function () {
  44. // Create a video element
  45. var video = document.createElement('video');
  46. video.id = 'myVideo';
  47. video.style.width = '100%';
  48. video.style.height = '100%';
  49. video.style.objectFit = 'contain'; // Ensure it covers the full area
  50. video.autoplay = true;
  51. video.controls = false; // Hide controls
  52. // Set the source of the video
  53. var source = document.createElement('source');
  54. source.src = './myvideo.mp4'; // Replace with the path to your video
  55. source.type = 'video/mp4';
  56. // Append source to video element
  57. video.appendChild(source);
  58. // Add the video to the container
  59. videoContainer.innerHTML = ''; // Clear previous content
  60. videoContainer.appendChild(video);
  61. video.onloadedmetadata = function () {
  62. console.log('Video metadata loaded successfully');
  63. console.log('Video duration:', video.duration);
  64. };
  65. video.onerror = function () {
  66. console.error('Error loading the video');
  67. };
  68. // Event listener for when the video ends
  69. video.addEventListener('ended', function () {
  70. // Revert to the original text
  71. videoContainer.innerHTML = '';
  72. container.appendChild(centeredText);
  73. document.write('Resource Exhausted !\n<br>You may reload the page or close the tab.');
  74. });
  75. });
  76. } else {
  77. seconds--;
  78. setTimeout(updateCountdown, 1000);
  79. }
  80. }
  81. updateCountdown();
  82. }
  83. closePageWithCountdown(1); // 1 sec for testing

字符串

展开查看全部

相关问题