html如何添加图像多媒体,HTML5中添加多媒体支持(三)

ok,最后我们来一段通过JavaScript创建并且控制播放Video的代码:

js代码:

$(function(){

// Stop if HTML5 video isn't supported

if (!document.createElement('video').canPlayType) {

$("#video_controls").hide();

return;

}

// 获取video对象

var video = document.getElementById("my_video");

// Play/Pause ============================//

$("#play_button").bind("click", function(){

video.play();

});

$("#pause_button").bind("click", function(){

video.pause();

});

$("#play_toggle").bind("click", function(){

if (video.paused) {

video.play();

$(this).html("Pause");

} else {

video.pause();

$(this).html("Play");

}

});

// 显示播放进度

// Play Progress ============================//

$(video).bind("timeupdate", function(){

var timePercent = (this.currentTime / this.duration) * 100;

$("#play_progress").css({ width: timePercent + "%" })

});

// Load Progress ============================//

$(video).bind("progress", function(){

updateLoadProgress();

});

$(video).bind("loadeddata", function(){

updateLoadProgress();

});

$(video).bind("canplaythrough", function(){

updateLoadProgress();

});

$(video).bind("playing", function(){

updateLoadProgress();

});

function updateLoadProgress() {

if (video.buffered.length > 0) {

var percent = (video.buffered.end(0) / video.duration) * 100;

$("#load_progress").css({ width: percent + "%" })

}

}

// Time Display =============================//

$(video).bind("timeupdate", function(){

$("#current_time").html(formatTime(this.currentTime));

});

$(video).bind("durationchange", function(){

$("#duration").html(formatTime(this.duration));

});

function formatTime(seconds) {

var seconds = Math.round(seconds);

var minutes = Math.floor(seconds / 60);

// Remaining seconds

seconds = Math.floor(seconds % 60);

// Add leading Zeros

minutes = (minutes >= 10) ? minutes : "0" + minutes;

seconds = (seconds >= 10) ? seconds : "0" + seconds;

return minutes + ":" + seconds;

}

});

css代码:

#video_controls {

width: 480px;

height: 30px;

background-color: #333;

color: #fff;

font-family: Verdana, sans-serif;

font-size: 12px;

text-transform: uppercase;

}

#video_controls div {

float: left;

height: 30px;

line-height: 30px;

}

.player-button {

width: 50px;

text-align: center;

cursor: pointer;

}

#progress {

position: relative;

background: #555;

width: 310px;

}

#play_progress {

position: absolute;

background: #6C3;

}

#load_progress {

position: absolute;

background: #69C;

}

#time {

width: 120px;

text-align: center;

}

html代码:

HTML5 Video Player

Your browser does not support HTML5 video.

Play

00:00 /

00:00

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值