chenbin92 / angularjs-video-playlist Goto Github PK
View Code? Open in Web Editor NEWThe html5 video playlist example for angularjs
The html5 video playlist example for angularjs
先来看看上面一张图,是我们经常会在一些视频网站看到的交互界面;有没有想一下它的实现是怎样的,跟着下面的步骤,让我们一起来了解 HTML5 Video API:
<video>
元素<video src="http://vjs.zencdn.net/v/oceans.mp4" controls="controls"></video>
这就是最简单的 video 使用,我们可以在网页得到一个视频,有默认的视频控制器,点击播放按钮可以播放,可以调节声音,播放进度等等;在很长一段时间里,我对 video
的使用也仅仅如此,它的效果如下:
在日常开发中,设计师往往不会给我们像上面那样的视频控制器的设计图,而是需要自定义视频控制器,整理了一张 HTML5 Video API 的脑图:
video
属性JavaScript:
playBtn.addEventListener('click', function(){
if (video.paused) {
video.play(); // 播放
} else {
video.pause(); // 暂停
}
}
video.addEventListener('timeupdate', function() {
var time = video.currentTime; // 视频当前播放时间
})
volumeBtn.addEventListener('click', function() {
if (video.muted) {
video.muted = false; // 打开声音
} else {
video.muted = true; // 关闭声音
}
})
// 在Hybrid app中HTML5 Video会默认使用系统的全屏播放,使用以下的方法可以禁止:
// 在 video 标签上添加 webkit-playsinline 属性
<video src="" webkit-playsinline></video>
// 设置 allowsInlineMediaPlayback 为 true
<preference name="AllowInlineMediaPlayback" value="true"/>
我们知道可以通过 video.currentTime
获取视频当前播放的时间,又可以通过 video.duration
获取视频总的时间长度,那么要做视频倒计时只需要写个计时器就可以了,其实不然,计时器也是不需要的,video API 提供了 timeupdate
事件,可以监听视频帧率的改动,这样一来环形倒计时就很简单了:
var totalDuration, currentTime;
totalDuration = Math.floor(video.duration);
video.addEventListener('timeupdate', function() {
currentTime = video.currentTime;
timer = totalDuration - currentTime;
})
上面介绍了 HTML5 Video API 的简单使用,可以看到 Video 提供了很多相关的属性和方法,方便开发者进行自定义;有时候,往往不需要开发者去写很多代码,一些框架或者库已经封装好了很多功能,当然,前提是你得知道最基础的知识点,才能更好的去使用
了解基本 API 之后,这里给大家介绍一个用 angularJS 编写的 HTML5 Video 库:Videogular
,稍后我们会使用 Videogular 来完成一个视频列表的功能
Videogular 将 HTML5 Video API 提供的相关接口封装成了单独的指令,开发者可以很方便的去自定义拼装视频组件,只需要下载安装对应的模块即可,可以使用 npm install xxx
和 bower install xxx
安装
Videogular
提供了很全的文档,下面是 Videogular
提供的 how to start 的一段代码,可以看到很多以 vg-xxx
开头的指令,这就是 Videogular
对 HTML5 Video API 进行封装之后的展现形式,一个视频组件主要包括三部分部分:
videogular
: 视频包装容器vg-media
:视频媒体资源vg-controls
: 视频控制器<div ng-app="myApp">
<div ng-controller="HomeCtrl as controller">
<videogular vg-theme="controller.config.theme">
<vg-media vg-src="controller.config.sources" // 视频资源
vg-tracks="controller.config.tracks">
</vg-media>
<vg-controls> // 自定义控制器
<vg-play-pause-button></vg-play-pause-button> // 播放按钮
<vg-time-display>{{ currentTime | date:'mm:ss' }}</vg-time-display> // 视频时间
<vg-scrub-bar> // 视频进度条
<vg-scrub-bar-current-time></vg-scrub-bar-current-time>
</vg-scrub-bar>
<vg-time-display>{{ timeLeft | date:'mm:ss' }}</vg-time-display> // 当前播放时间
<vg-volume> // 声音按钮
<vg-mute-button></vg-mute-button>
<vg-volume-bar></vg-volume-bar>
</vg-volume>
<vg-fullscreen-button></vg-fullscreen-button> // 全屏按钮
</vg-controls>
<vg-overlay-play></vg-overlay-play>
<vg-poster vg-url='controller.config.plugins.poster'></vg-poster> // 视频封面
</videogular>
</div>
</div>
request_method url
orders
,单个资源的 root_key 是单数形式,如 order
POST /api/v1/users
return one user data
user: {
id: 1,
firstname: "bruce",
lastname: "chen",
email: " [email protected]",
height: "172cm",
weight: "56kg",
created_at: "2016-03-08T07:41:15.000Z",
updated_at: "2016-04-022T07:41:15.000Z"
}
PUT /api/v1/users/:id
return one user data
user: {
id: 1,
firstname: "bruce",
lastname: "chen",
email: " [email protected]",
height: "172cm",
weight: "56kg",
created_at: "2016-03-08T07:41:15.000Z",
updated_at: "2016-04-022T07:41:15.000Z"
}
GET /api/v1/categories
return an array of workout categories data
categories: [
{
id: 1,
name: "Arms",
created_at: "2016-03-08T07:41:15.000Z",
updated_at: "2016-04-022T07:41:15.000Z"
},
{
id: 2,
name: "Abs",
created_at: "2016-03-08T07:41:15.000Z",
updated_at: "2016-04-022T07:41:15.000Z"
},
{
id: 3,
name: "Butt",
created_at: "2016-03-08T07:41:15.000Z",
updated_at: "2016-04-022T07:41:15.000Z"
},
{
id: 4,
name: "Legs",
created_at: "2016-03-08T07:41:15.000Z",
updated_at: "2016-04-022T07:41:15.000Z"
},
{
id: 5,
name: "Full-body",
created_at: "2016-03-08T07:41:15.000Z",
updated_at: "2016-04-022T07:41:15.000Z"
}
]
GET /api/categories/:id/videos
return an array of video lists
videos: [
{
id: 1,
position: 1,
title: "Jumping Jacks",
description: "some description",
file: "http://vjs.zencdn.net/v/oceans.mp4",
length: 100,
burned_calories: 20,
created_at: "2016-03-08T07:41:15.000Z",
updated_at: "2016-04-022T07:41:15.000Z"
},
{
id: 2,
position: 2,
title: "Jumping Jacks",
description: "some description",
file: "http://vjs.zencdn.net/v/oceans.mp4",
length: 100,
burned_calories: 20,
created_at: "2016-03-08T07:41:15.000Z",
updated_at: "2016-04-022T07:41:15.000Z"
}
]
GET /api/categories/:id/videos/:id
return a video details
video: {
id: 1,
position: 1,
title: "Jumping Jacks",
description: "some description",
file: "http://vjs.zencdn.net/v/oceans.mp4",
length: 100,
burned_calories: 20,
created_at: "2016-03-08T07:41:15.000Z",
updated_at: "2016-04-022T07:41:15.000Z"
}
提交锻炼的数据
POST /api/v1/users/:id/workouts
return one user workouts data
workout: {
id: 1,
burned_calories: 100,
accumulated_time: "30min",
created_at: "2016-03-08T07:41:15.000Z",
updated_at: "2016-04-022T07:41:15.000Z"
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.