diff options
author | pacien | 2024-01-05 01:53:30 +0100 |
---|---|---|
committer | pacien | 2024-01-05 01:53:30 +0100 |
commit | 0bf2af62baeb7912d6274bab4ff6d7f83dd692ee (patch) | |
tree | 49628f18a158081f492227cd8e3ba58b884eba7c | |
parent | b562cf4a27f7e1f8f15d772da156b9b28a7f3119 (diff) | |
download | echoclip-0bf2af62baeb7912d6274bab4ff6d7f83dd692ee.tar.gz |
only play one audio clip at once
-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 => { |