HTML5学习总结-04 音频&视频播放

一 音频播放

1 Audio(音频)

  HTML5提供了播放音频文件的标准

2 control(控制器)

  control属性攻添加播放,暂停和音量空间。

3 标签定义声音

<audio>

例子:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <button id="btn" onclick="playMusic()">播放</button>
        <button id="mutedBtn" onclick="muteMusic()">静音</button>
        <audio id="audio" src="source/Morning.mp3"  controls="controls" >您的浏览器不支持标签</audio>
        <script>
            function playMusic(){
                var audioObj = document.getElementById("audio");
                var btn = document.getElementById("btn");
                
                console.log("step1 audioObj.paused="+audioObj.paused);
                
                if( audioObj.paused){
                    btn.innerText ="暂停";
                    audioObj.play()
                }else{
                    btn.innerText ="播放";
                    audioObj.pause();
                }
            }
            
            function muteMusic(){
                var audioObj = document.getElementById("audio");
                var mutedBtn = document.getElementById("mutedBtn");
                console.log( audioObj.muted);
                
                if( audioObj.muted ){
                    audioObj.muted= "";
                    mutedBtn.innerText= "取消静音"
                }else{
                    audioObj.muted= "muted";
                    mutedBtn.innerText= "静音"
                }
                
            }
            
        </script>
    </body>
</html>

二 视频播放

1 Video(视频)

  HTML5提供了播放视频文件的标准

2 control(控制器)

  control属性攻添加播放,暂停和音量空间。

3 标签定义声音

<video>

4 属性

  width: 宽

  height: 高

5 例子

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<video controls="controls">您的浏览器不支持标签
<source src="source/video1.mp4"></source>
<source src="source/video1.ogv"></source>
</video>
</body>
</html>

三 video捕获摄像头画面

1 通过video元素调用摄像头捕获画面

<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
<style>
#myDiv {}
</style> </head> <body>
<div>
<video width="" height="" autoplay></video>
<canvas width="" height=""></canvas>
<br>
<button id="drawBtn" style="width: 100%;height: 50px;">拍照</button>
</div> <script>
var video = document.querySelector('video');
var canvas = document.querySelector('canvas'); // video捕获摄像头画面
navigator.webkitGetUserMedia({
video: true
}, success, error); function success(stream) {
video.src = window.webkitURL.createObjectURL(stream);
video.play();
} function error(err) {
alert('video error: ' + err)
} //canvas
var context = canvas.getContext('2d'); setTimeout(function() {
//把当前视频帧内容渲染到画布上
context.drawImage(video, , , , );
}, ); var button= document.getElementById('drawBtn');
button.onclick = function(){
context.drawImage(video, , , , );
} </script>
</body> </html>

  运行页面后,浏览器出于安全性考虑,会询问是否允许当前页面访问你的摄像头设备,点击“允许”后便能直接在 <video> 上看到摄像头捕获到的画面了:

HTML5学习总结-04 音频&视频播放

  直接在页面中显示base64 编码的图片

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body> <img src='data:image/jpg;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCACBAQ4DASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3+iiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiobm7trKAz3dxFBCvWSVwqj8TTSbdkBNRUNtd217AJ7S4inhbpJE4ZT+Iqahpp2YBRRRSAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiqGtazZ6BpU2o38hWCIcgcsx7KB3JqoxcpKMVdsNi/XgPxa1y41DxbJpu8i0sAqog6FyoLMffnH4e9drpHxj0m/1JbW9spbGJztSdpA6j03YA2/rXE/FjRZ7DxbLqOxjaXyq6SY+XcFAK59eM/jX0GUYWeHxlq8bNp2/D9DCrJSh7ovwl1y40/wAWx6bvJtL8MroegcKSrD34x+PtXRfFnxleWl4ugadcNABGHupI2wxz0TPYY5PrkVznwm0S41DxdFqGxha2Ks7vjguQQq59ec/QUz4tafPa+Obi6kRhDdxxvG3Y7UCkfmv616E6VCpmqvuo3+f/AAxmnJUih4H8WX2geIrbNw7WVxKsdxE7EqVJxu+oznNfSNfNHgXw9P4i8UWsKRk20Miy3L44VAc4PucYH/1q+l68ziBUlXjy/FbX9DShe2oUUUV4BuFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUVzHxDuriy8C6ncWk8sE6CPbJE5VlzIoOCORwamcuWLl2NaFJ1qsaS+00vvdjp6K+abHVvGep+Z/Z+oa/d+Xjf9nmmk256ZweOh/Krnm/ESEGRj4mVVGSXE+APxrjWNT15WfQy4blF8rrRTPoqivGPBPxP1L+1bfTNclFzbzsIknKgPGxOBkjqM9c89817PXTSrRqq8Tx8fl9bA1PZ1euzWzCiiitThCvKvjd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KACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAP/2Q=='/>
</body>
</html>

  1)data:image/png;base64是什么?

参考 http://blog.csdn.net/c_mihoo/article/details/12774719

  Base64 主要不是加密,它主要的用途是把一些二进制数转成普通字符用于网络传输。由于一些二进制字符在传输协议中属于控制字符,不能直接传送需要转换一下。针对图片进行Base64编码后,减少一次网络请求。但也不缓存图片了。

  2)在线图片转Base64

http://tool.css-js.com/base64.html

  3)java实现图片与base64字符串之间的转换

package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; public class Base64Test
{ //图片转化成base64字符串
public static String GetImageStr()
{//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = "e://quant.png";//待处理的图片
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try
{
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);//返回Base64编码过的字节数组字符串
} //base64字符串转化成图片
public static boolean GenerateImage(String imgStr)
{ //对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) //图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try
{
//Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for(int i=;i<b.length;++i)
{
if(b[i]<)
{//调整异常数据
b[i]+=;
}
}
//生成jpeg图片
String imgFilePath = "e://111.png";//新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
}
catch (Exception e)
{
return false;
}
} public static void main(String[] args)
{
String strImg = GetImageStr();
System.out.println(strImg); } }

2 上传画面到服务器

var curFrame;   //当前帧

function savePic(){
         console.log("curFrame=\n"+curFrame);
             
         $.ajax({
                url : '/TestH5/SnapPicAction',
                type : "POST",
                data : {
                    'date': '告警' + Date.now() ,
                    'pic': '<img src="' + curFrame + '" />'
                },
                success: function(){
                    console.log('submit done')
                    
                },
                error: function(err){
                   // cache.reqTime = 0;
                    console.log('error: ' + err)
                }
            });
        
}

  把捕获的数据帧发送到后台。完整代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
#myDiv { }
</style>
</head> <body>
<div>
<video width="" height="" autoplay></video>
<canvas width="" height=""></canvas>
<br>
<button id="drawBtn" style="width: 50%;height: 50px;">拍照</button>
<button id="savePicBtn" style="width: 50%;height: 50px;">保存</button>
</div> <script src="js/jquery-3.1.0.js"></script>
<script>
var video = document.querySelector('video');
var canvas = document.querySelector('canvas'); var curFrame; //当前帧 // video捕获摄像头画面
navigator.webkitGetUserMedia({
video : true
}, success, error); function success(stream) {
video.src = window.webkitURL.createObjectURL(stream);
video.play();
} function error(err) {
alert('video error: ' + err)
} //canvas
var context = canvas.getContext('2d'); setTimeout(function() {
//把当前视频帧内容渲染到画布上
context.drawImage(video, , , , );
}, ); var button = document.getElementById('drawBtn');
button.onclick = function() {
context.drawImage(video, , , , );
} $(document).ready(function() { }); //捕获并保存帧内容
function captureAndSaveFrame(){
context.drawImage(video, , , , );
curFrame = canvas.toDataURL(); //转为base64并保存
} function savePic(){
console.log("curFrame=\n"+curFrame); $.ajax({
url : '/TestH5/SnapPicAction',
type : "POST",
data : {
'date': '告警' + Date.now() ,
'pic': '<img src="' + curFrame + '" />'
},
success: function(){
console.log('submit done') },
error: function(err){
// cache.reqTime = 0;
console.log('error: ' + err)
}
}); } var savePicBtn = document.getElementById('savePicBtn');
savePicBtn.onclick = function() {
captureAndSaveFrame();
savePic();
} </script>
</body> </html>

3 保存数据帧的后台业务逻辑

SnapPicAction.java

package com.mobile.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.mobile.util.Base64; /**
* Servlet implementation class SnapPicAction
*/
public class SnapPicAction extends HttpServlet {
private static final long serialVersionUID = 1L; public SnapPicAction() {
super();
} protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("--- doGet SnapPicAction"); response.getWriter().append("Served at: ").append(request.getContextPath());
} protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf8"); System.out.println("--- doPost SnapPicActionn --------------------");
String date = request.getParameter("date");
String pic = request.getParameter("pic");
System.out.println("***step0 pic=" + pic);
int beginIdx = pic.indexOf("\"") + ;
int lastIdx = pic.lastIndexOf("\""); String base64Pic = pic.substring(beginIdx, lastIdx).replace("data:image/png;base64,", ""); decodeBase64ToImage(base64Pic, "E://temp3//", "aaa.png"); } public static void decodeBase64ToImage(String base64, String path, String imgName) {
try {
FileOutputStream write = new FileOutputStream(new File(path + imgName));
byte[] decoderBytes = Base64.decode(base64);
write.write(decoderBytes);
write.close();
} catch (IOException e) {
e.printStackTrace();
}
} }

Base64.java

package com.mobile.util;

public class Base64 {
/**
* Base64编码表。
*/
private static final char[] BASE64CODE = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '', '', '', '',
'', '', '', '', '', '', '+', '/', }; /**
* Base64解码表。
*/
private static final byte[] BASE64DECODE = { -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, // 注意两个63,为兼容SMP,
-, -, -, -, -, -, -, -, -, -, -, , -, , -, , // “/”和“-”都翻译成63。
, , , , , , , , , , -, -, -, , -, -, -, , , , , , , , , , , , , ,
, , // 注意两个0:
, , , , , , , , , , , -, -, -, -, -, // “A”和“=”都翻译成0。
-, , , , , , , , , , , , , , , , , , , , , , , , , , ,
-, -, -, -, -, }; private static final int HEX_255 = 0x0000ff; private static final int HEX_16515072 = 0xfc0000; private static final int HEX_258048 = 0x3f000; private static final int HEX_4032 = 0xfc0; private static final int HEX_63 = 0x3f; private static final int HEX_16711680 = 0xff0000; private static final int HEX_65280 = 0x00ff00; private static final int NUMBER_TWO = ; private static final int NUMBER_THREE = ; private static final int NUMBER_FOUR = ; private static final int NUMBER_SIX = ; private static final int NUMBER_EIGHT = ; private static final int NUMBER_TWELVE = ; private static final int NUMBER_SIXTEEN = ; private static final int NUMBER_EIGHTEEN = ; /**
* 构造方法私有化,防止实例化。
*/
private Base64() {
} /**
* Base64编码。将字节数组中字节3个一组编码成4个可见字符。
*
* @param b
* 需要被编码的字节数据。
* @return 编码后的Base64字符串。
*/
public static String encode(byte[] b) {
int code = ; // 按实际编码后长度开辟内存,加快速度
StringBuffer sb = new StringBuffer(((b.length - ) / NUMBER_THREE) << NUMBER_TWO + NUMBER_FOUR); // 进行编码
for (int i = ; i < b.length; i++) {
code |= (b[i] << (NUMBER_SIXTEEN - i % NUMBER_THREE * NUMBER_EIGHT))
& (HEX_255 << (NUMBER_SIXTEEN - i % NUMBER_THREE * NUMBER_EIGHT));
if (i % NUMBER_THREE == NUMBER_TWO || i == b.length - ) {
sb.append(BASE64CODE[(code & HEX_16515072) >>> NUMBER_EIGHTEEN]);
sb.append(BASE64CODE[(code & HEX_258048) >>> NUMBER_TWELVE]);
sb.append(BASE64CODE[(code & HEX_4032) >>> NUMBER_SIX]);
sb.append(BASE64CODE[code & HEX_63]);
code = ;
}
} // 对于长度非3的整数倍的字节数组,编码前先补0,编码后结尾处编码用=代替,
// =的个数和短缺的长度一致,以此来标识出数据实际长度
if (b.length % NUMBER_THREE > ) {
sb.setCharAt(sb.length() - , '=');
}
if (b.length % NUMBER_THREE == ) {
sb.setCharAt(sb.length() - NUMBER_TWO, '=');
}
return sb.toString();
} /**
* Base64解码。
*
* @param code
* 用Base64编码的ASCII字符串
* @return 解码后的字节数据
*/
public static byte[] decode(String code) {
// 检查参数合法性
if (code == null) {
return null;
}
int len = code.length();
if (len % NUMBER_FOUR != ) {
throw new IllegalArgumentException("Base64 string length must be 4*n");
}
if (code.length() == ) {
return new byte[];
} // 统计填充的等号个数
int pad = ;
if (code.charAt(len - ) == '=') {
pad++;
}
if (code.charAt(len - NUMBER_TWO) == '=') {
pad++;
} // 根据填充等号的个数来计算实际数据长度
int retLen = len / NUMBER_FOUR * NUMBER_THREE - pad; // 分配字节数组空间
byte[] ret = new byte[retLen]; // 查表解码
char ch1, ch2, ch3, ch4;
int i;
for (i = ; i < len; i += NUMBER_FOUR) {
int j = i / NUMBER_FOUR * NUMBER_THREE;
ch1 = code.charAt(i);
ch2 = code.charAt(i + );
ch3 = code.charAt(i + NUMBER_TWO);
ch4 = code.charAt(i + NUMBER_THREE);
int tmp = (BASE64DECODE[ch1] << NUMBER_EIGHTEEN) | (BASE64DECODE[ch2] << NUMBER_TWELVE)
| (BASE64DECODE[ch3] << NUMBER_SIX) | (BASE64DECODE[ch4]);
ret[j] = (byte) ((tmp & HEX_16711680) >> NUMBER_SIXTEEN);
if (i < len - NUMBER_FOUR) {
ret[j + ] = (byte) ((tmp & HEX_65280) >> NUMBER_EIGHT);
ret[j + NUMBER_TWO] = (byte) ((tmp & HEX_255)); } else {
if (j + < retLen) {
ret[j + ] = (byte) ((tmp & HEX_65280) >> NUMBER_EIGHT);
}
if (j + NUMBER_TWO < retLen) {
ret[j + NUMBER_TWO] = (byte) ((tmp & HEX_255));
}
}
}
return ret;
}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>TestH5</display-name> <servlet>
<servlet-name>SnapPicAction</servlet-name>
<servlet-class>com.mobile.action.SnapPicAction</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>SnapPicAction</servlet-name>
<url-pattern>/SnapPicAction</url-pattern>
</servlet-mapping> </web-app>

jquery CDN

<script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js">
</script>

参考资料:

data:image/png;base64是什么

http://blog.csdn.net/c_mihoo/article/details/12774719

上一篇:linux内存——/proc/sys/vm/drop_caches


下一篇:Sql Server系列:Select基本语句