-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
180 lines (166 loc) · 6.49 KB
/
index.html
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.initVideo{
width : 640px;
height: 480px;
z-index: 999;
left:30%;
position: fixed;
margin: auto;
}
</style>
</head>
<body>
<video class="initVideo"></video>
<button id="start">开始</button>
<button id="end">结束</button>
<script>
var MediaUtils = {
/**
* 获取用户媒体设备(处理兼容的问题)
* @param videoEnable {boolean} - 是否启用摄像头
* @param audioEnable {boolean} - 是否启用麦克风
* @param callback {Function} - 处理回调
*/
getUserMedia: function (videoEnable, audioEnable, callback) {
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia
|| navigator.msGetUserMedia || window.getUserMedia;
var constraints = { video: videoEnable, audio: audioEnable };
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
callback(false, stream);
})['catch'](function (err) {
callback(err);
});
} else if (navigator.getUserMedia) {
navigator.getUserMedia(constraints, function (stream) {
callback(false, stream);
}, function (err) {
callback(err);
});
} else {
callback(new Error('Not support userMedia'));
}
},
/**
* 关闭媒体流
* @param stream {MediaStream} - 需要关闭的流
*/
closeStream: function (stream) {
if (typeof stream.stop === 'function') {
stream.stop();
}
else {
let trackList = [stream.getAudioTracks(), stream.getVideoTracks()];
for (let i = 0; i < trackList.length; i++) {
let tracks = trackList[i];
if (tracks && tracks.length > 0) {
for (let j = 0; j < tracks.length; j++) {
let track = tracks[j];
if (typeof track.stop === 'function') {
track.stop();
}
}
}
}
}
}
};
// 用于存放 MediaRecorder 对象和音频Track,关闭录制和关闭媒体设备需要用到
var recorder, mediaStream;
// 用于存放录制后的音频文件对象和录制结束回调
var recorderFile, stopRecordCallback;
// 用于存放是否开启了视频录制
var videoEnabled = false;
// 录制短语音
function startRecord(enableVideo) {
videoEnabled = enableVideo;
MediaUtils.getUserMedia(enableVideo, true, function (err, stream) {
if (err) {
throw err;
} else {
// 通过 MediaRecorder 记录获取到的媒体流
recorder = new MediaRecorder(stream);
mediaStream = stream;
var chunks = [], startTime = 0;
recorder.ondataavailable = function (e) {
chunks.push(e.data);
};
recorder.onstop = function (e) {
recorderFile = new Blob(chunks, { 'type': recorder.mimeType });
chunks = [];
if (null != stopRecordCallback) {
stopRecordCallback();
}
};
recorder.start();
}
});
}
// 停止录制
function stopRecord(callback) {
stopRecordCallback = callback;
// 终止录制器
recorder.stop();
// 关闭媒体流
MediaUtils.closeStream(mediaStream);
}
// 播放录制的音频
function playRecord() {
console.log(recorderFile);
alert(recorderFile.type+'-----'+recorderFile.size)
var url = URL.createObjectURL(recorderFile);
var dom = document.createElement(videoEnabled ? 'video' : 'audio');
dom.autoplay = true;
dom.src = url;
if (videoEnabled) {
dom.width = 640;
dom.height = 480;
dom.style.zIndex = '9999999';
dom.style.position = 'fixed';
dom.style.left = '0';
dom.style.right = '0';
dom.style.top = '0';
dom.style.bottom = '0';
dom.style.margin = 'auto';
document.body.appendChild(dom);
}
}
// 启动录制视频 (若需要录制音频参数指定为 false 即可)
function startPlay() {
var constraints = { audio: true, video: { width: 1280, height: 720 } };
navigator.mediaDevices.getUserMedia(constraints)
.then(function (mediaStream) {
var video = document.querySelector('video');
video.style.display='block'
video.srcObject = mediaStream;
video.onloadedmetadata = function (e) {
video.play();
};
})
.catch(function (err) { console.log(err.name + ": " + err.message); });
startRecord(true);
}
function stopPlay() {
var video = document.querySelector('video');
video.style.display='none';
stopRecord(function () {
// 播放
playRecord();
});
}
document.querySelector('#start').onclick = () => {
startPlay()
}
document.querySelector('#end').onclick = () => {
stopPlay()
}
</script>
</body>
</html>