-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
75 lines (48 loc) · 1.85 KB
/
script.js
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
startButton.addEventListener("click", function () {
navigator.mediaDevices
.getUserMedia({ audio: true })
.then(function (stream) {
audioContext = new AudioContext();
var audioInput = audioContext.createMediaStreamSource(stream);
recorder = new Recorder(audioInput);
// Bắt đầu ghi âm
recorder.record();
// Disable nút "Bắt đầu ghi âm" và enable nút "Dừng ghi âm"
startButton.disabled = true;
stopButton.disabled = false;
console.log("Bắt đầu ghi âm...");
})
.catch(function (err) {
console.error("Lỗi truy cập microphone:", err);
});
});
stopButton.addEventListener("click", function () {
// Dừng ghi âm
recorder.stop();
// Enable nút "Bắt đầu ghi âm" và disable nút "Dừng ghi âm"
startButton.disabled = false;
stopButton.disabled = true;
console.log("Dừng ghi âm...");
// Lấy dữ liệu âm thanh ghi âm và xử lý nó
recorder.exportWAV(function (blob) {
// Tạo URL từ blob
const url = URL.createObjectURL(blob);
// Cập nhật source của audio element với URL mới
const recordedAudio = document.getElementById("recordedAudio");
recordedAudio.src = url;
playButton.addEventListener("click", function () {
// Phát lại file ghi âm
const recordedAudio = document.getElementById("recordedAudio");
recordedAudio.play();
});
playButton.disabled = false;
downloadButton.disabled = false;
// Thêm đoạn code tải xuống:
downloadButton.addEventListener("click", function () {
const link = document.createElement("a");
link.href = url;
link.download = "recording.wav"; // Đổi thành tên file mong muốn
link.click();
});
});
});