diff options
-rw-r--r-- | app.js | 25 |
1 files changed, 21 insertions, 4 deletions
@@ -11,6 +11,25 @@ const autoplayCheckbox = document.querySelector("#autoplay"); | |||
11 | const clearBtn = document.querySelector("#clear"); | 11 | const clearBtn = document.querySelector("#clear"); |
12 | const clips = document.querySelector("#clips"); | 12 | const clips = document.querySelector("#clips"); |
13 | 13 | ||
14 | function stopPlayer(player) { | ||
15 | player.pause(); | ||
16 | player.currentTime = 0; | ||
17 | } | ||
18 | |||
19 | function stopOtherPlayersExcept(player) { | ||
20 | document.querySelectorAll("audio").forEach(other => { | ||
21 | if (other != player) | ||
22 | stopPlayer(other); | ||
23 | }); | ||
24 | } | ||
25 | |||
26 | function makeExclusive(player) { | ||
27 | player.addEventListener("play", _event => { | ||
28 | stopOtherPlayersExcept(player); | ||
29 | }); | ||
30 | return player; | ||
31 | } | ||
32 | |||
14 | function wrapElement(wrapper, child) { | 33 | function wrapElement(wrapper, child) { |
15 | const wrapperElement = document.createElement(wrapper); | 34 | const wrapperElement = document.createElement(wrapper); |
16 | wrapperElement.append(child); | 35 | wrapperElement.append(child); |
@@ -41,7 +60,7 @@ function onGetDeviceSuccess(stream) { | |||
41 | recordingIndicator.classList.remove("active"); | 60 | recordingIndicator.classList.remove("active"); |
42 | const blob = new Blob(recordingChunks, { type: mediaRecorder.mimeType }); | 61 | const blob = new Blob(recordingChunks, { type: mediaRecorder.mimeType }); |
43 | recordingChunks = []; | 62 | recordingChunks = []; |
44 | const audioElement = audioElementForBlob(blob); | 63 | const audioElement = makeExclusive(audioElementForBlob(blob)); |
45 | 64 | ||
46 | // TODO: record blob and list to local persistent storage | 65 | // TODO: record blob and list to local persistent storage |
47 | // TODO: "clear all" button to clear all clips | 66 | // TODO: "clear all" button to clear all clips |
@@ -50,10 +69,8 @@ function onGetDeviceSuccess(stream) { | |||
50 | 69 | ||
51 | clips.prepend(wrapElement("li", audioElement)); | 70 | clips.prepend(wrapElement("li", audioElement)); |
52 | 71 | ||
53 | if (autoplayCheckbox.checked) { | 72 | if (autoplayCheckbox.checked) |
54 | // TODO: stop playing any other clip | ||
55 | audioElement.play(); | 73 | audioElement.play(); |
56 | } | ||
57 | }); | 74 | }); |
58 | 75 | ||
59 | recordBtn.addEventListener("mousedown", _event => { | 76 | recordBtn.addEventListener("mousedown", _event => { |