gpt4 book ai didi

jquery - 为自定义 HTML5 和 jQuery 视频播放器持续显示时间

转载 作者:行者123 更新时间:2023-11-28 01:54:55 25 4
gpt4 key购买 nike

当我首次加载包含我制作的自定义嵌入式视频播放器的页面时,已用时间和总时间未显示。但是,当我播放视频时,它出现了。请任何人帮助我并告诉我我可能做错了什么。谢谢!

示例图片:

页面首次加载时视频播放器的外观: What the Video Player looks like when page first loads.

视频播放器播放时的样子: What the Video Player looks like when played.

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Video Player - HTML5</title>
<link rel="stylesheet" href="css/video_player.css">
</head>
<body>
<section id="main">
<video id="videoPlayer" width="640" height="480">
<source src="video/test.webm" type="video/webm"></source>
<source src="video/test.ogv" type="video/ogv"></source>
<source src="video/test.mp4" type="video/mp4"></source>
This is a video for html5 browsers.
</video>

<nav id="vidControls" class="clearfix">
<div id="playPauseBtn" class="left">Play</div>
<div id="scrubberContainer" class="left">
<div id="scrubberBar"></div>
</div>
<div id="timer" class="left">
<span></span>
</div>
</nav>
</section>

<!-- JAVASCRIPT -->
<script src="js/jquery-2.0.3.js"></script>
<script src="js/VideoPlayer.js"></script>
<script>
$(document).ready(function(){
VP.init();
});
</script>
<!-- END JAVASCRIPT -->

</body>
</html>

CSS:

html, body {
padding: 0;
margin: 0;
width: 100%;
}

article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {
display: block;
}

.left {
float: left;
}

.right {
float: right;
}

.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}

.clearfix {
display: inline-block;
}

#main {
width: 720px;
margin: 20px auto;
padding: 12px;
background: #CCC;
text-align: center;
border-radius: 5px;
-moz-box-shadow: 2px 5px 5px #888;
-webkit-box-shadow: 2px 5px 5px #888;
box-shadow: 2px 5px 5px #888;
/* For IE 8 */
-ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=5, Direction=135, Color='#888')";
/* For IE 5.5 - 7 */
filter: progid:DXImageTransform.Microsoft.Shadow(Strength=5, Direction=135, Color='#888');
/* For Non-CSS3 Browsers */
background: #EBF1F6;
/* For IE */
filter:
progid:DXImageTransform.Microsoft.gradient(startColorstr='#F3F4F5', endColorstr='#DADADA');
/* For WebKit Browsers */
background:
-webkit-gradient(linear, left top, left bottom, from(#F3F4F5), to(#DADADA));
/* For Firefox 3.6+ */
background:
-moz-linear-gradient(top, #F3F4F5, #DADADA);
}

nav {
margin: 5px;
width: 100%;
}

#timer {
display: block;
color: #000;
text-align: center;
height: 28px;
padding-top: 12px;
font-size: 12px;
font-family: Arial;
}

#playPauseBtn {
display: block;
background: #656565;
color: #FFF;
text-align: center;
padding: 9px;
border: 1px solid #DADADA;
cursor: pointer;
width: 64px;
}

#playPauseBtn:hover {
background: #FFF;
color: #333;
}

#playPauseBtn:active {
background: #333;
color: #999;
}

#scrubberContainer {
position: relative;
width: 500px;
height: 20px;
padding: 4px;
margin: 6px;
background: #000;
}

#scrubberBar {
position: absolute;
background: #FF6600;
width: 0px;
height: 20px;
}

Javascript:

var VP = VP || {};

var barSize = new Number(500);
var scrubberContainer = $('#scrubberContainer')[0];
var scrubberBar = $('#scrubberBar')[0];
var playPauseBtn = $('#playPauseBtn')[0];
var videoPlayer = $('#videoPlayer')[0];
var updatePlayer;
var updateTimer;

VP.init = function(){
playPauseBtn.addEventListener('click', VP.togglePlayPause, false);
scrubberContainer.addEventListener('click', VP.scrubberClicked, false);
videoPlayer.addEventListener('timeupdate', VP.updateTimer);
}

VP.togglePlayPause = function(){
if(!videoPlayer.paused && !videoPlayer.ended){
videoPlayer.pause();
$('#playPauseBtn').html('Play');
window.clearInterval(updatePlayer);
window.clearInterval(updateTimer);
} else {
videoPlayer.play();
$('#playPauseBtn').html('Pause');
updatePlayer = setInterval(function()
{
VP.updateScrubber();
}, 100);
}
}

VP.updateScrubber = function(){
if(!videoPlayer.ended){
var size = parseInt(videoPlayer.currentTime * barSize/videoPlayer.duration);
$('#scrubberBar').css('width', size + 'px');
}
else{
$('#scrubberBar').css('width', '0px');
$('#playPauseBtn').css('Play');
window.clearInterval(updatePlayer);
}
};

VP.scrubberClicked = function(e){
if(!videoPlayer.paused && !videoPlayer.ended){
var mouseX = e.pageX - scrubberContainer.offsetLeft;
var newTime = mouseX * videoPlayer.duration/barSize;
videoPlayer.currentTime = newTime;
$('#scrubberBar').css('width', mouseX + 'px');
}
};

VP.updateTimer = function(){
var eTime;
var tTime;

//calculate elapsed time
var eSeconds = Math.round(videoPlayer.currentTime);
var eMinutes = Math.floor(eSeconds/60);
eMinutes = (eMinutes >= 10) ? eMinutes : "0" + eMinutes;
eSeconds = Math.floor(eSeconds % 60);
eSeconds = (eSeconds >= 10) ? eSeconds : "0" + eSeconds;

//calculate total time
var tSeconds = Math.round(videoPlayer.duration);
var tMinutes = Math.floor(tSeconds/60);
tMinutes = (tMinutes >= 10) ? tMinutes : "0" + tMinutes;
tSeconds = Math.floor(tSeconds % 60);
tSeconds = (tSeconds >= 10) ? tSeconds : "0" + tSeconds;

//assign these values to our variables
eTime = '' + eMinutes + ':' + eSeconds;
tTime = '' + tMinutes + ':' + tSeconds;

//display in timer
$('#timer').html('' + eTime + ' / ' + tTime);
};

最佳答案

这有点草率,当 html5 视频播放器发展时可能会有更好的方法,但它对我来说仍然有效。只需将其放在 document.ready 代码的末尾...

$("#videoPlayer")[0].play();
setTimeout(function() {
$("#videoPlayer")[0].pause();
}, 100);

它开始播放视频,然后在 1/10 秒后停止。查看该页面的人应该不会注意到任何东西,并且它有足够的时间来显示视频的播放信息。

关于jquery - 为自定义 HTML5 和 jQuery 视频播放器持续显示时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17489189/

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