HTML5 <Audio/>标签Api整理(二)
1.实例2:
相对较完整
Html代码:
<style>
#volumeSlider .slider-selection {
background:#bababa;
}
</style>
<div class="container">
<p>../content/audio/海阔天空.mp3</p>
<button class="btn btn-primary" id="playBtn">
<i class="glyphicon glyphicon-play"></i>
</button>
<button class="btn btn-info" id="mutedBtn">
<i class="glyphicon glyphicon-volume-down"></i>
</button>
<div class="form-group">
<label class="control-label">音量:</label>
<input class="form-control" id="volume" data-slider-id="volumeSlider"
data-slider-min="0" data-slider-max="100" data-slider-step="1"
/>
</div>
<div class="form-group">
<label class="control-label">进度:</label>
<input class="form-control" id="timeBtn" data-slider-id="volumeSlider"
data-slider-min="0" data-slider-step="0.01" />
</div>
</div>
<audio id="myAudio"></audio>
Js代码:
var currentFile = '../content/audio/海阔天空.mp3';
//判断浏览器是否支持audio
if (!window.HTMLAudioElement) {
alert('您的浏览器不支持audio标签');
} else {
var myAudio = document.getElementById('myAudio');
myAudio.autoplay = false;
myAudio.preload = false;
if (myAudio.src.length <= 0) {
myAudio.src = currentFile;
}
//播放/暂停按钮
$('#playBtn').click(function () {
var icon = $(this).find('i');
if (myAudio.paused) {
myAudio.play();
icon.removeClass('glyphicon-play').addClass('glyphicon-pause');
} else {
myAudio.pause();
icon.addClass('glyphicon-play').removeClass('glyphicon-pause');
}
});
//静音按钮
$('#mutedBtn').click(function () {
var icon = $(this).find('i');
icon.toggleClass('glyphicon-volume-down').toggleClass('glyphicon-volume-off');
myAudio.muted = !myAudio.muted;
});
//音量按钮
$('#volume').slider({
value: myAudio.volume * 100
}).on('change', function (e) {
var value = e.value.newValue / 100;
myAudio.volume = value;
});
//播放进度按钮
$('#timeBtn').slider({
value: 0,
tooltip: 'always',
formatter: function (value) {
var date = new Date(0, 0, 0, 0, 0, value);
return '当前时间:' + date.Format('mm:ss');
}
}).on('change', function (e) {
var value = e.value.newValue;
myAudio.currentTime = value;
myAudio.play();
});
//进入可播放状态
myAudio.oncanplay = function () {
console.info('进入可播放状态,音频总长度:' + myAudio.duration);
$('#timeBtn').slider('setAttribute', 'max', myAudio.duration);
}
myAudio.onplay = function () {
console.info('开始播放:' + myAudio.currentTime);
}
myAudio.onpause = function () {
console.info('暂停播放:' + myAudio.currentTime);
}
myAudio.ontimeupdate = function (e) {
$('#timeBtn').slider('setValue', myAudio.currentTime);
}
//此事件和onplay好像一直
myAudio.onplaying = function () {
console.info('正在播放:' + myAudio.currentTime);
}
//此事件和oncanplay 好像一直
myAudio.onloadedmetadata = function () {
console.info('文件加载成功:' + myAudio.duration);
}
}
显示结果:

更多事件实例参考:HTML5 Media事件(包含缓存进度显示)
时间格式化处理
// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
HTML5 <Audio/>标签Api整理(二)的更多相关文章
- HTML5 <Audio>标签API整理(三)
一.浏览器支持 Internet Explorer 9+, Firefox, Opera, Chrome, 和 Safari 都支持 <audio> 元素. 注意: Internet Ex ...
- HTML5 <Audio>标签API整理(一)
简单实例: <audio id="myAudio"></audio> <script> var myAudio = document.getEl ...
- HTML5 Audio标签方法和函数API介绍
问说网 > 文章教程 > 网页制作 > HTML5 Audio标签方法和函数API介绍 Audio APIHTML5HTML5 Audio预加载 HTML5 Audio标签方法和函数 ...
- CEF3 HTML5 audio标签为什么不能播放mp3格式的音频文件
CEF3 HTML5 audio标签 为什么不能播放mp3格式的音频文件 原因略. 解决方法: 找一个最新版的chrome ,我用的是24版本.路径 C:\Documents and Sett ...
- html5 audio标签微信部分苹果手机不能自动播放音乐终极解决方案
html5 audio标签微信部分苹果手机不能自动播放音乐终极解决方案 大家都知道需要在点击时候后 播放 ps:如果点击ajax 回来播放也不行,必须点击立即播放 要背景自动播放只能采取下面方案< ...
- html5 audio标签切换播放音乐的方法
html5 audio标签切换播放音乐的方法<pre><audio id="music1" preload loop="loop">&l ...
- ios加载html5 audio标签用js无法自动播放
html5 audio标签在ios 微信浏览器中是无法自动播放的,最近在做一个小的项目遇到这个问题,安卓和pc都是正常的,唯独ios不行,查阅了很多资料,找到了以下方法,也许不是最好用的方法,如果有更 ...
- 论HTML5 Audio 标签歌词同步的实现
HTML5草案里面其实有原生的字幕标签(<track> Tag)的,但使用的是vtt格式的文件,非常规的字幕(.sub, .srt)或歌词文件(.lrc). 用法如下(代码来自W3Scho ...
- html5 -----audio标签
在现在的公司上班需要做一个html5页面,上下可以滑动的,在页面上需要音乐,默认开始音乐播放,点击音乐标签后音乐停止.后来在项目开发中,遇到性能优化的问题,所以我建议大家以后在使用时不要直接在页面中使 ...
随机推荐
- 转载:用Dreamweave cs 5.5+PhoneGap+Jquery Mobile搭建移动开发
转载地址:http://blog.csdn.net/haha_mingg/article/details/7900221 移动设备应用开发有多难,只要学会HTML5+Javascript就可以.用Dr ...
- Ecshop开发
http://www.cnblogs.com/xcxc/category/579565.html
- 直接将视频文件原码流转换成YUV,输出到屏幕显示
#include "stdafx.h" #define inline _inline#ifndef INT64_C#define INT64_C(c) (c ## LL)#defi ...
- MVVM in Depth
这篇文章开始粗略的介绍了软件开发中松耦合的概念并讲述了使用MVC.MVP和MVVM三种模式达到松耦合.然后分析了这三种模式适用范围,其中: MVC(Model-View-Controller)适用于w ...
- View的getLeft, getRight, getTop, getBottom
View的getLeft, getRight, getTop, getBottom方法得到的分别是相对于其父组件原点坐标不同方向的距离 网上找了张图说明: 其中right和left的计算方法如下: r ...
- [cocos2d] 调用动画方法
利用texture atlases生成动画 中讲到如何添加动画,如果想要调用已添加的动画怎么办? 在1.0.1版本以前的cocos2d添加动画的方法为: CCAnimation *anim = [CC ...
- QQ游戏百万人同时在线服务器架构实现
转载自:http://morton5555.blog.163.com/blog/static/976407162012013112545710/# QQ游戏于前几日终于突破了百万人同时在线的关口,向着 ...
- poj2151
求每只队伍都回答出题目,且至少有一只队伍回答出n道题的概率存在性问题我们可以转化为任意性问题用P(每支队伍都回答出题目)-P(每只队伍回答的题目数小于n)然后我们可以递推求解 ..,..,..] of ...
- 怎么打开Windows Server 2008 图片预览的功能?
打开一个文件夹,点击菜单中的“工具”->“文件夹选项”,切换到“查看”选项卡,在高级设置中取消如下选项: “始终显示图标,从不显示缩略图” “在缩略图上显示文件图标”
- poj 2975 Nim(博弈)
Nim Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5232 Accepted: 2444 Description N ...