网页录像录音功能的实现之MediaRecorder的使用
前面介绍了通过H5实现在网页内打开摄像头和麦克风,实现截图和图像预览的相关知识。
getUserMedia API及HTML5 调用摄像头和麦克风
一个简单的demo 实现摄像头的调用及视频录制,截图,下载等功能,逐渐完善中。。。。
地址:https://github.com/zhangdexian/videorecorder
页面效果:

但是我们无法将将数据保存到本地甚至上传到我们自己的服务器,本篇主要是针对录像录音的保存做一个简单的介绍和学习。
首先来看一下官方的文档介绍:
构造器 ConstructorSection
MediaRecorder()
Creates a new MediaRecorder object, given a MediaStream to record. Options are available to do things like set the container's MIME type (such as "video/webm" or "video/mp4") and the bit rates of the audio and video tracks or a single overall bit rate.
构造器接受一个MediaStream流对象,可以看到还可以配置MIME type类型及比特率。
属性 PropertiesSection
MediaRecorder.mimeType Read only // 返回MediaRecorder实例的mimetype类型 只读
Returns the MIME type that was selected as the recording container for the MediaRecorder object when it was created.
MediaRecorder.state Read only // 返回MediaRecorder当前状态:闲置、录制中和暂停 只读
Returns the current state of the MediaRecorder object (inactive, recording, or paused.)
MediaRecorder.stream Read only // 返回所录制的流对象 只读
Returns the stream that was passed into the constructor when the MediaRecorder was created.
MediaRecorder.ignoreMutedMedia // 当媒体流静音时是否忽略
Indicates whether the MediaRecorder instance will record input when the input MediaStreamTrack is muted. If this attribute is false, MediaRecorder will record silence for audio and black frames for video. The default is false.
MediaRecorder.videoBitsPerSecond Read only // 返回视频的比特率 只读
Returns the video encoding bit rate in use. This may differ from the bit rate specified in the constructor (if it was provided).
MediaRecorder.audioBitsPerSecond Read only // 返回音频的比特率 只读
Returns the audio encoding bit rate in use. This may differ from the bit rate specified in the constructor (if it was provided).
方法 MethodsSection
MediaRecorder.isTypeSupported() // 返回给定的MIME type是否被当前客户端支持
Returns a Boolean value indicating if the given MIME type is supported by the current user agent .
MediaRecorder.pause() // 暂定录制
Pauses the recording of media.
MediaRecorder.requestData() // 返回录制的二进制数据,调用这个方法后会生成一个新的Blob对象
Requests a Blob containing the saved data received thus far (or since the last time requestData() was called. After calling this method, recording continues, but in a new Blob.
MediaRecorder.resume() // 恢复录制
Resumes recording of media after having been paused.
MediaRecorder.start() // 开始录制
Begins recording media; this method can optionally be passed a timeslice argument with a value in milliseconds. If this is specified, the media will be captured in separate chunks of that duration, rather than the default behavior of recording the media in a single large chunk.
MediaRecorder.stop() // 停止录制
Stops recording, at which point a dataavailable event containing the final Blob of saved data is fired. No more recording occurs.
事件 Event HandlersSection
MediaRecorder.ondataavailable // 有可用数据流时触发,e.data即时需要的视频数据
Called to handle the dataavailable event, which is periodically triggered each time timeslice milliseconds of media have been recorded (or when the entire media has been recorded, if timeslice wasn't specified). The event, of type BlobEvent, contains the recorded media in its data property. You can then collect and act upon that recorded media data using this event handler.
MediaRecorder.onerror
An EventHandler called to handle the error event, including reporting errors that arise with media recording. These are fatal errors that stop recording. The received event is based on the MediaRecorderErrorEvent interface, whose error property contains a DOMException that describes the actual error that occurred.
MediaRecorder.onpause
An EventHandler called to handle the pause event, which occurs when media recording is paused.
MediaRecorder.onresume
An EventHandler called to handle the resume event, which occurs when media recording resumes after being paused.
MediaRecorder.onstart
An EventHandler called to handle the start event, which occurs when media recording starts.
MediaRecorder.onstop
An EventHandler called to handle the stop event, which occurs when media recording ends, either when the MediaStream ends — or after the MediaRecorder.stop() method is called.
下面是一个简单的例子:
if (navigator.mediaDevices) {
console.log('getUserMedia supported.');
var constraints = { audio: true };
var chunks = [];
navigator.mediaDevices.getUserMedia(constraints)
.then(function(stream) {
var mediaRecorder = new MediaRecorder(stream);
visualize(stream);
record.onclick = function() {
mediaRecorder.start();
console.log(mediaRecorder.state);
console.log("recorder started");
record.style.background = "red";
record.style.color = "black";
}
stop.onclick = function() {
mediaRecorder.stop();
console.log(mediaRecorder.state);
console.log("recorder stopped");
record.style.background = "";
record.style.color = "";
}
mediaRecorder.onstop = function(e) {
console.log("data available after MediaRecorder.stop() called.");
var clipName = prompt('Enter a name for your sound clip');
var clipContainer = document.createElement('article');
var clipLabel = document.createElement('p');
var audio = document.createElement('audio');
var deleteButton = document.createElement('button');
clipContainer.classList.add('clip');
audio.setAttribute('controls', '');
deleteButton.innerHTML = "Delete";
clipLabel.innerHTML = clipName;
clipContainer.appendChild(audio);
clipContainer.appendChild(clipLabel);
clipContainer.appendChild(deleteButton);
soundClips.appendChild(clipContainer);
audio.controls = true;
var blob = new Blob(chunks, { 'type' : 'audio/ogg; codecs=opus' });
chunks = [];
var audioURL = URL.createObjectURL(blob);
audio.src = audioURL;
console.log("recorder stopped");
deleteButton.onclick = function(e) {
evtTgt = e.target;
evtTgt.parentNode.parentNode.removeChild(evtTgt.parentNode);
}
}
mediaRecorder.ondataavailable = function(e) {
chunks.push(e.data);
}
})
.catch(function(err) {
console.log('The following error occurred: ' + err);
})
}
网页录像录音功能的实现之MediaRecorder的使用的更多相关文章
- delphi android 录像(使用了JMediaRecorder,MediaRecorder的使用方法)
delphi xe系列自带的控件都无法保存录像,经网友帮忙,昨天终于实现了录像功能(但有个问题是录像时无画面显示),程序主要使用了JMediaRecorder,MediaRecorder的使用方法可参 ...
- delphi android 录像(使用了JMediaRecorder,MediaRecorder的使用方法可参考网上java的相关说明)
delphi xe系列自带的控件都无法保存录像,经网友帮忙,昨天终于实现了录像功能(但有个问题是录像时无画面显示),程序主要使用了JMediaRecorder,MediaRecorder的使用方法可参 ...
- Android 自带 camera 详解
在本文中 需要考虑的问题 概述 Manifest声明 使用内置的摄像头应用程序 捕获图像的intent 捕获视频的intent 接收摄像头intent的结果 创建摄像头应用程序 检测摄像头硬件 访问摄 ...
- websocket聊天体验(二)
上一篇说到后续可以支持:最近历史.表情+图片,顺便还实现了简易的音频和视频.暂时没有实现实时语音对讲,有待后续再研究.点开在线聊天页面,即可看到最近历史记录(18条). 聊天的底层数据都是基于txt文 ...
- 录像时调用MediaRecorder的start()时发生start failed: -19错误
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- Android调用手机摄像头使用MediaRecorder录像并播放
最近在项目开发中需要调用系统的摄像头录像并播放. 在开发中遇到了两个问题,记录下: (1)开发过程中出现摄像头占用,启动失败,报错.但是我已经在onDestory()中关闭了资源. 报错原因:打开程序 ...
- android Camera拍照 及 MediaRecorder录像 预览图像差90度
Camera拍照: 今天做照相机程序,结果写好了发现出问题了,预览的图像差90度.相关源代码如下: Camera.Parameters params = camera.getParameters(); ...
- MediaRecorder录像那些事
最近在做一个项目需要运用到MediaRecorder的API,之前都没接触过这部分,开始着手弄的时候各种各样的问题,真是让人崩溃呀! 最后通过网上的资料和大神的指点,当然也有自己几天坚持不懈的努力,终 ...
- Android App调用MediaRecorder实现录音功能的实例【转】
本文转载自:http://www.jb51.net/article/82281.htm 这篇文章主要介绍了Android App调用MediaRecorder实现录音功能的实例,MediaRecord ...
随机推荐
- Django学习---笔记一
一. 新建虚拟机在虚拟中完成项目 1.新建虚拟机 mkvirtalenv 虚拟机名称 2.进入虚拟机 cd 新建的虚拟机名称 3.安装Django pip install django 4.Dj ...
- windows下更改Mac地址
方法一.在桌面上的“网上邻居”图标上单击右键,选择“属性”,在弹出的“网络连接”的对话框中,在“本地连接”图标上单击右键,选择“属性”,会弹出一个“本地连接属性”的对话框,单击“配置”按钮,选择“高级 ...
- Linux 小知识翻译 - 「虚拟化技术」
这次聊聊「虚拟化技术」. 虚拟化技术,有时简称为「虚拟化」,最近经常听人说它.但是却不太清楚它的意思.到底虚拟了什么东西?本来是用来干什么的? 有名的虚拟化软件要数 VMware 和 VirtualB ...
- 实现strStr()的golang实现
实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...
- k8s部署rocketmq 双主
由于apache 官网的 docker image 是单点,要实现集群方式部署. rocketmq 分为 nameserver 和 broker , 对于之间调用频繁的服务,会增加网络压力, 所以 考 ...
- Qt 编程指南 3 信号和槽沟通
https://qtguide.ustclug.org/ 1 信号和槽 所谓信号槽,简单来说,就像是插销一样:一个插头和一个插座.怎么说呢?当某种事件发生之后,比如,点击了一下鼠标,或者按了某个按键, ...
- 利用原生JS实现网页1920banner图滚动效果
内容描述:随着PC设备硬件性能的进步和分辨率的不断提高,现在主流网站逐渐开始采用1920banner图,为适应这一趋势,博主设计了1920banner图的滚动效果,代码利用了原生JS实现了1920ba ...
- 转载 AutoFac常见用法总结
第二节:框架前期准备篇之AutoFac常见用法总结 一. 说在前面的话 凡是大约工作在两年以上的朋友们,或多或少都会接触到一些框架搭建方面的知识,只要一谈到框架搭建这个问题或者最佳用法这个问题,势 ...
- robotframework接口测试(一)—Get request json
(前提:引入了 requests.requestsLibrary等相关库,这样才可以只有相关的关键字.) 理想中的过程: 1. 创建session 2. 在该session下发起请求 3. 验证返回结 ...
- python3 day03 大纲
一. 上次课内容回顾 1. 格式化输出 %d 占位数字 %s 占位字符串 %f 占位小数 "jfklasjkfj%s %d %f" % (值1, 值2, 值3) "{}爱 ...