-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathchange-audio-output.html
195 lines (165 loc) · 5.53 KB
/
change-audio-output.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Audio Output Example - Record Plugin for Video.js</title>
<link href="../node_modules/video.js/dist/video-js.min.css" rel="stylesheet">
<link href="../node_modules/videojs-wavesurfer/dist/css/videojs.wavesurfer.min.css" rel="stylesheet">
<link href="../dist/css/videojs.record.css" rel="stylesheet">
<link href="assets/css/examples.css" rel="stylesheet">
<script src="../node_modules/video.js/dist/video.min.js"></script>
<script src="../node_modules/recordrtc/RecordRTC.js"></script>
<script src="../node_modules/webrtc-adapter/out/adapter.js"></script>
<script src="../node_modules/wavesurfer.js/dist/wavesurfer.min.js"></script>
<script src="../node_modules/wavesurfer.js/dist/plugin/wavesurfer.microphone.min.js"></script>
<script src="../node_modules/videojs-wavesurfer/dist/videojs.wavesurfer.min.js"></script>
<script src="../dist/videojs.record.js"></script>
<script src="browser-workarounds.js"></script>
<style>
body {
font-weight: 300;
}
/* change player background color */
#myAudio {
background-color: #8EDBE6;
}
select {
margin: 0 0 0.5em 0;
}
.outputSelector {
margin-top: 30px;
display: none;
}
</style>
</head>
<body>
<div>
<audio id="myAudio" class="video-js vjs-default-skin"></audio>
<div class="outputSelector">
<label>Select audio output: </label>
<select id="selector"></select>
</div>
</div>
<script>
/* eslint-disable */
var devices, deviceId;
var options = {
controls: true,
bigPlayButton: false,
width: 600,
height: 300,
fluid: false,
plugins: {
wavesurfer: {
backend: 'WebAudio',
waveColor: 'black',
progressColor: '#2E732D',
displayMilliseconds: true,
debug: true,
cursorWidth: 1,
hideScrollbar: true,
plugins: [
// enable microphone plugin
WaveSurfer.microphone.create({
bufferSize: 4096,
numberOfInputChannels: 1,
numberOfOutputChannels: 1,
constraints: {
video: false,
audio: true
}
})
]
},
record: {
audio: true,
video: false,
maxLength: 20,
displayMilliseconds: true,
debug: true
}
}
};
var outputSection = document.getElementsByClassName('outputSelector')[0];
// apply some workarounds for certain browsers
applyAudioWorkaround();
var player = videojs('myAudio', options, function() {
// print version information at startup
var msg = 'Using video.js ' + videojs.VERSION +
' with videojs-record ' + videojs.getPluginVersion('record') +
', videojs-wavesurfer ' + videojs.getPluginVersion('wavesurfer') +
', wavesurfer.js ' + WaveSurfer.VERSION + ' and recordrtc ' +
RecordRTC.version;
videojs.log(msg);
// enumerate devices at startup
player.record().enumerateDevices();
});
player.on('enumerateReady', function() {
// show output selector section
outputSection.style.display = 'block';
devices = player.record().devices;
// handle selection changes
var outputSelector = document.getElementById('selector');
outputSelector.addEventListener('change', changeAudioOutput);
// populate select options
var deviceInfo, option, i;
for (i = 0; i !== devices.length; ++i) {
deviceInfo = devices[i];
option = document.createElement('option');
option.value = deviceInfo.deviceId;
if (deviceInfo.kind === 'audiooutput') {
console.info('Found audio output device: ', deviceInfo.label);
option.text = deviceInfo.label || 'speaker ' +
(outputSelector.length + 1);
outputSelector.appendChild(option);
}
}
if (outputSelector.length == 0) {
// no output devices found, disable select
option = document.createElement('option');
option.text = 'No audio output devices found';
option.value = undefined;
outputSelector.appendChild(option);
outputSelector.disabled = true;
console.warn(option.text);
} else {
console.info('Total audio output devices found:', outputSelector.length);
}
});
function changeAudioOutput(event) {
deviceId = event.target.value;
try {
// change audio output device
player.record().setAudioOutput(deviceId);
} catch (error) {
console.warn(error);
// jump back to first output device in the list as it's the default
event.target.selectedIndex = 0;
}
}
player.on('audioOutputReady', function() {
console.log('Changed audio output to deviceId:', deviceId);
});
// error handling
player.on('enumerateError', function() {
console.warn('enumerate error:', player.enumerateErrorCode);
});
player.on('deviceError', function() {
console.warn('device error:', player.deviceErrorCode);
});
player.on('error', function(element, error) {
console.error(error);
});
// user clicked the record button and started recording
player.on('startRecord', function() {
console.log('started recording!');
});
// user completed recording and stream is available
player.on('finishRecord', function() {
// the blob object contains the recorded data that
// can be downloaded by the user, stored on server etc.
console.log('finished recording: ', player.recordedData);
});
</script>
</body>
</html>