【问题标题】:HTML5 video player stuck at posterHTML5 视频播放器卡在海报上
【发布时间】:2016-09-06 03:57:21
【问题描述】:

我正在尝试为 HTML5 视频制作自己的自定义控件。到目前为止,我只实现了一个播放/暂停按钮,但我遇到了问题。

视频在启动时不播放,所以当我点击自定义播放按钮时,我可以听到视频的音频 - 但视频卡在海报图片上。

我的演示有一个 JSFiddle 的问题(尝试点击播放按钮):https://jsfiddle.net/9gpg6gbd/

这是播放/暂停按钮代码的 sn-p:

// Initialize play button
if (video.paused) {
    container.querySelector("#supervideo-playbutton").innerHTML = "►"
} else {
    container.querySelector("#supervideo-playbutton").innerHTML = "| |"
}
container.querySelector("#supervideo-playbutton").addEventListener("click", function(){
    if (video.paused) {
        video.play();
        container.querySelector("#supervideo-playbutton").innerHTML = "| |"
    } else {
        video.pause();
        container.querySelector("#supervideo-playbutton").innerHTML = "►"
    }
});

任何帮助将不胜感激,我很难过这个。

【问题讨论】:

  • 查看最后的编辑。

标签: javascript html


【解决方案1】:

你必须犯错误:

  1. 不要在视频标签内添加 HTML 元素。
  2. 请勿将视频元素保存在变量中并更改其位置。

解决方案 #1:

用选择器替换你的video.play()

function initializeControls(container, video) {
	// Initialize play button
	if (video.paused) {
        container.querySelector("#supervideo-playbutton").innerHTML = "►"
    } else {
        container.querySelector("#supervideo-playbutton").innerHTML = "| |"
    }
    container.querySelector("#supervideo-playbutton").addEventListener("click", function(){
    	if (document.body.querySelectorAll(".supervideo")[0].paused) {
    		document.body.querySelectorAll(".supervideo")[0].play();
    		container.querySelector("#supervideo-playbutton").innerHTML = "| |"
    	} else {
      	document.body.querySelectorAll(".supervideo")[0].pause();
    		container.querySelector("#supervideo-playbutton").innerHTML = "►"
    	}
	});
}

function createVidElement() {
	var videos = document.body.querySelectorAll(".supervideo");

	[].forEach.call(videos, function(video) {
		// Hide controls if the player has tdem
		if (video.hasAttribute("controls")) {
			video.controls = false;
		}
		// Create video container
		var container = document.createElement('div');
    	container.setAttribute("class", "supervideo-container");
    	video.parentElement.appendChild(container);
    	container.appendChild(video);
    	// Created media controls
    	container.innerHTML = container.innerHTML + '<table cellpadding="0" cellspacing="0" id="supervideo-controls"><tr> <td><button id="supervideo-playbutton" type="button">&#215;</button></td> <td>Time</td> <td>Scrollbar</td> <td>Mute button</td> <td>Volume</td> <td>Fullscreen</td> </tr></table>';
    	initializeControls(container, video);
	});
}

createVidElement();
<video width="100%" controls class="supervideo">
  <source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" />
</video>

解决方案 #2:

在 foreach 中,您更改了 &lt;Video&gt; 标记的位置。您将其附加到新容器中。在此行之后,您的选择器将无效,您必须重新分配您的选择器。

function initializeControls(container, video) {
	// Initialize play button
	if (video.paused) {
        container.querySelector("#supervideo-playbutton").innerHTML = "&#9658;"
    } else {
        container.querySelector("#supervideo-playbutton").innerHTML = "| |"
    }
    container.querySelector("#supervideo-playbutton").addEventListener("click", function(){
    	if (video.paused) {
    		video.play();
    		container.querySelector("#supervideo-playbutton").innerHTML = "| |"
    	} else {
    		video.pause();
    		container.querySelector("#supervideo-playbutton").innerHTML = "&#9658;"
    	}
	});
}

function createVidElement() {
	var videos = document.body.querySelectorAll(".supervideo");

	[].forEach.call(videos, function(video) {
		// Hide controls if the player has tdem
		if (video.hasAttribute("controls")) {
			video.controls = false;
		}
		// Create video container
		var container = document.createElement('div');
    	container.setAttribute("class", "supervideo-container");
    	video.parentElement.appendChild(container);
    	
      //container.appendChild(video);
    	
      // Created media controls
    	container.innerHTML = container.innerHTML + '<table cellpadding="0" cellspacing="0" id="supervideo-controls"><tr> <td><button id="supervideo-playbutton" type="button">&#215;</button></td> <td>Time</td> <td>Scrollbar</td> <td>Mute button</td> <td>Volume</td> <td>Fullscreen</td> </tr></table>';
    	initializeControls(container, video);
	});
}

createVidElement();
<video width="100%" controls class="supervideo">
  <source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" />
</video>

【讨论】:

  • 无法暂停视频
  • 查看解决方案 #2
  • 视频选择器也有同样的问题,我忘了把if(video. paused)改成document.body.querySelectorAll(".supervideo")[0].paused
  • 非常感谢,解决方案 #2 解决了我的问题。
猜你喜欢
  • 2011-07-13
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 1970-01-01
  • 2019-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多