Spaces:
Running
Running
Update static/register_record.js
Browse files- static/register_record.js +150 -150
static/register_record.js
CHANGED
@@ -1,150 +1,150 @@
|
|
1 |
-
let mediaRecorder;
|
2 |
-
let audioChunks = [];
|
3 |
-
let userCount = 0; // 追加されたメンバー数を保持
|
4 |
-
let isRecording = false; // 録音中かどうかを判定するフラグ
|
5 |
-
let currentRecordingButton = null; // 現在録音中のボタンを保持
|
6 |
-
let userNames = [];
|
7 |
-
|
8 |
-
function toggleRecording(button) {
|
9 |
-
button.classList.toggle("recording");
|
10 |
-
}
|
11 |
-
|
12 |
-
async function startRecording(button) {
|
13 |
-
if (isRecording && currentRecordingButton !== button) return; // 他の人が録音中なら何もしない
|
14 |
-
isRecording = true; // 録音中に設定
|
15 |
-
currentRecordingButton = button; // 録音中のボタンを記録
|
16 |
-
|
17 |
-
try {
|
18 |
-
const stream = await navigator.mediaDevices.getUserMedia({
|
19 |
-
audio: true,
|
20 |
-
});
|
21 |
-
mediaRecorder = new MediaRecorder(stream, { mimeType: "audio/webm" });
|
22 |
-
audioChunks = [];
|
23 |
-
mediaRecorder.ondataavailable = (e) => audioChunks.push(e.data);
|
24 |
-
mediaRecorder.onstop = () => {
|
25 |
-
sendAudioChunks(audioChunks, button); // ボタン情報を渡す
|
26 |
-
audioChunks = [];
|
27 |
-
isRecording = false; // 録音停止後はフラグを戻す
|
28 |
-
currentRecordingButton = null; // 録音ボタンを解除
|
29 |
-
};
|
30 |
-
mediaRecorder.start();
|
31 |
-
toggleRecording(button);
|
32 |
-
} catch (err) {
|
33 |
-
console.error("マイクアクセスに失敗しました:", err);
|
34 |
-
isRecording = false; // エラー発生時もフラグを戻す
|
35 |
-
currentRecordingButton = null;
|
36 |
-
}
|
37 |
-
}
|
38 |
-
|
39 |
-
function stopRecording(button) {
|
40 |
-
if (!isRecording) return; // 録音中でない場合は停止しない
|
41 |
-
mediaRecorder.stop();
|
42 |
-
toggleRecording(button);
|
43 |
-
}
|
44 |
-
|
45 |
-
function handleRecording(e) {
|
46 |
-
const button = e.target.closest(".record-button");
|
47 |
-
if (button) {
|
48 |
-
if (isRecording && currentRecordingButton !== button) {
|
49 |
-
// 他の人が録音中なら反応しない
|
50 |
-
return;
|
51 |
-
}
|
52 |
-
if (mediaRecorder && mediaRecorder.state === "recording") {
|
53 |
-
stopRecording(button);
|
54 |
-
} else {
|
55 |
-
startRecording(button);
|
56 |
-
}
|
57 |
-
}
|
58 |
-
}
|
59 |
-
|
60 |
-
function sendAudioChunks(chunks, button) {
|
61 |
-
// 引数に button を追加
|
62 |
-
const audioBlob = new Blob(chunks, { type: "audio/wav" });
|
63 |
-
const reader = new FileReader();
|
64 |
-
reader.onloadend = () => {
|
65 |
-
const base64String = reader.result.split(",")[1]; // Base64エンコードされた音声データ
|
66 |
-
// フォームの取得方法を変更
|
67 |
-
const form = button.closest(".user-item")?.querySelector("form")
|
68 |
-
const nameInput = form?.querySelector('input[name="name"]');
|
69 |
-
const name = nameInput ? nameInput.value : "unknown"; // 名前がない
|
70 |
-
fetch("/upload_base_audio", {
|
71 |
-
method: "POST",
|
72 |
-
headers: {
|
73 |
-
"Content-Type": "application/json",
|
74 |
-
},
|
75 |
-
body: JSON.stringify({ audio_data: base64String, name: name }),
|
76 |
-
})
|
77 |
-
.then((response) => response.json())
|
78 |
-
.then((data) => {
|
79 |
-
// エラー処理のみ残す
|
80 |
-
if (data.error) {
|
81 |
-
alert("エラー: " + data.error);
|
82 |
-
console.error(data.details);
|
83 |
-
}
|
84 |
-
// 成功時の処理(ボタンの有効化など)
|
85 |
-
else {
|
86 |
-
console.log("音声データ送信成功:", data);
|
87 |
-
userNames.push(name);
|
88 |
-
// 必要に応じて、ここでUIの変更(ボタンの有効化など)を行う
|
89 |
-
// 例: button.disabled = true; // 送信ボタンを無効化
|
90 |
-
// 例: button.classList.remove("recording"); //録音中のスタイルを解除
|
91 |
-
}
|
92 |
-
})
|
93 |
-
.catch((error) => {
|
94 |
-
console.error("エラー:", error);
|
95 |
-
});
|
96 |
-
};
|
97 |
-
reader.readAsDataURL(audioBlob);
|
98 |
-
}
|
99 |
-
|
100 |
-
//
|
101 |
-
function
|
102 |
-
window.location.href = "index";
|
103 |
-
}
|
104 |
-
|
105 |
-
// Add user function
|
106 |
-
function addUser() {
|
107 |
-
const userName = prompt("ユーザー名を入力してください");
|
108 |
-
if (userName) {
|
109 |
-
const userList = document.getElementById("people-list");
|
110 |
-
const userDiv = document.createElement("div");
|
111 |
-
userDiv.classList.add(
|
112 |
-
"user-item", // 追加
|
113 |
-
"bg-gray-700",
|
114 |
-
"p-4",
|
115 |
-
"rounded-lg",
|
116 |
-
"text-white",
|
117 |
-
"flex",
|
118 |
-
"justify-between",
|
119 |
-
"items-center",
|
120 |
-
"flex-wrap", // 追加
|
121 |
-
"gap-3" // 追加
|
122 |
-
);
|
123 |
-
userDiv.innerHTML = `
|
124 |
-
<form
|
125 |
-
action="/submit"
|
126 |
-
method="POST"
|
127 |
-
class="flex items-center space-x-2 w-full sm:w-auto"
|
128 |
-
onsubmit="event.preventDefault();"
|
129 |
-
>
|
130 |
-
<input
|
131 |
-
type="text"
|
132 |
-
name="name"
|
133 |
-
placeholder="名前を入力"
|
134 |
-
value="${userName}"
|
135 |
-
class="flex-1 px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 bg-gray-700 text-white"
|
136 |
-
/>
|
137 |
-
<button type="button" class="record-button" aria-label="音声録音開始">
|
138 |
-
<div class="record-icon"></div>
|
139 |
-
</button>
|
140 |
-
</form>
|
141 |
-
`;
|
142 |
-
userDiv
|
143 |
-
.querySelector(".record-button")
|
144 |
-
.addEventListener("click", handleRecording);
|
145 |
-
userList.appendChild(userDiv);
|
146 |
-
userCount++;
|
147 |
-
}
|
148 |
-
}
|
149 |
-
|
150 |
-
document.getElementById("add-btn").addEventListener("click", addUser);
|
|
|
1 |
+
let mediaRecorder;
|
2 |
+
let audioChunks = [];
|
3 |
+
let userCount = 0; // 追加されたメンバー数を保持
|
4 |
+
let isRecording = false; // 録音中かどうかを判定するフラグ
|
5 |
+
let currentRecordingButton = null; // 現在録音中のボタンを保持
|
6 |
+
let userNames = [];
|
7 |
+
|
8 |
+
function toggleRecording(button) {
|
9 |
+
button.classList.toggle("recording");
|
10 |
+
}
|
11 |
+
|
12 |
+
async function startRecording(button) {
|
13 |
+
if (isRecording && currentRecordingButton !== button) return; // 他の人が録音中なら何もしない
|
14 |
+
isRecording = true; // 録音中に設定
|
15 |
+
currentRecordingButton = button; // 録音中のボタンを記録
|
16 |
+
|
17 |
+
try {
|
18 |
+
const stream = await navigator.mediaDevices.getUserMedia({
|
19 |
+
audio: true,
|
20 |
+
});
|
21 |
+
mediaRecorder = new MediaRecorder(stream, { mimeType: "audio/webm" });
|
22 |
+
audioChunks = [];
|
23 |
+
mediaRecorder.ondataavailable = (e) => audioChunks.push(e.data);
|
24 |
+
mediaRecorder.onstop = () => {
|
25 |
+
sendAudioChunks(audioChunks, button); // ボタン情報を渡す
|
26 |
+
audioChunks = [];
|
27 |
+
isRecording = false; // 録音停止後はフラグを戻す
|
28 |
+
currentRecordingButton = null; // 録音ボタンを解除
|
29 |
+
};
|
30 |
+
mediaRecorder.start();
|
31 |
+
toggleRecording(button);
|
32 |
+
} catch (err) {
|
33 |
+
console.error("マイクアクセスに失敗しました:", err);
|
34 |
+
isRecording = false; // エラー発生時もフラグを戻す
|
35 |
+
currentRecordingButton = null;
|
36 |
+
}
|
37 |
+
}
|
38 |
+
|
39 |
+
function stopRecording(button) {
|
40 |
+
if (!isRecording) return; // 録音中でない場合は停止しない
|
41 |
+
mediaRecorder.stop();
|
42 |
+
toggleRecording(button);
|
43 |
+
}
|
44 |
+
|
45 |
+
function handleRecording(e) {
|
46 |
+
const button = e.target.closest(".record-button");
|
47 |
+
if (button) {
|
48 |
+
if (isRecording && currentRecordingButton !== button) {
|
49 |
+
// 他の人が録音中なら反応しない
|
50 |
+
return;
|
51 |
+
}
|
52 |
+
if (mediaRecorder && mediaRecorder.state === "recording") {
|
53 |
+
stopRecording(button);
|
54 |
+
} else {
|
55 |
+
startRecording(button);
|
56 |
+
}
|
57 |
+
}
|
58 |
+
}
|
59 |
+
|
60 |
+
function sendAudioChunks(chunks, button) {
|
61 |
+
// 引数に button を追加
|
62 |
+
const audioBlob = new Blob(chunks, { type: "audio/wav" });
|
63 |
+
const reader = new FileReader();
|
64 |
+
reader.onloadend = () => {
|
65 |
+
const base64String = reader.result.split(",")[1]; // Base64エンコードされた音声データ
|
66 |
+
// フォームの取得方法を変更
|
67 |
+
const form = button.closest(".user-item")?.querySelector("form")
|
68 |
+
const nameInput = form?.querySelector('input[name="name"]');
|
69 |
+
const name = nameInput ? nameInput.value : "unknown"; // 名前がない
|
70 |
+
fetch("/upload_base_audio", {
|
71 |
+
method: "POST",
|
72 |
+
headers: {
|
73 |
+
"Content-Type": "application/json",
|
74 |
+
},
|
75 |
+
body: JSON.stringify({ audio_data: base64String, name: name }),
|
76 |
+
})
|
77 |
+
.then((response) => response.json())
|
78 |
+
.then((data) => {
|
79 |
+
// エラー処理のみ残す
|
80 |
+
if (data.error) {
|
81 |
+
alert("エラー: " + data.error);
|
82 |
+
console.error(data.details);
|
83 |
+
}
|
84 |
+
// 成功時の処理(ボタンの有効化など)
|
85 |
+
else {
|
86 |
+
console.log("音声データ送信成功:", data);
|
87 |
+
userNames.push(name);
|
88 |
+
// 必要に応じて、ここでUIの変更(ボタンの有効化など)を行う
|
89 |
+
// 例: button.disabled = true; // 送信ボタンを無効化
|
90 |
+
// 例: button.classList.remove("recording"); //録音中のスタイルを解除
|
91 |
+
}
|
92 |
+
})
|
93 |
+
.catch((error) => {
|
94 |
+
console.error("エラー:", error);
|
95 |
+
});
|
96 |
+
};
|
97 |
+
reader.readAsDataURL(audioBlob);
|
98 |
+
}
|
99 |
+
|
100 |
+
// 録音画面に移動
|
101 |
+
function showRecorder() {
|
102 |
+
window.location.href = "index";
|
103 |
+
}
|
104 |
+
|
105 |
+
// Add user function
|
106 |
+
function addUser() {
|
107 |
+
const userName = prompt("ユーザー名を入力してください");
|
108 |
+
if (userName) {
|
109 |
+
const userList = document.getElementById("people-list");
|
110 |
+
const userDiv = document.createElement("div");
|
111 |
+
userDiv.classList.add(
|
112 |
+
"user-item", // 追加
|
113 |
+
"bg-gray-700",
|
114 |
+
"p-4",
|
115 |
+
"rounded-lg",
|
116 |
+
"text-white",
|
117 |
+
"flex",
|
118 |
+
"justify-between",
|
119 |
+
"items-center",
|
120 |
+
"flex-wrap", // 追加
|
121 |
+
"gap-3" // 追加
|
122 |
+
);
|
123 |
+
userDiv.innerHTML = `
|
124 |
+
<form
|
125 |
+
action="/submit"
|
126 |
+
method="POST"
|
127 |
+
class="flex items-center space-x-2 w-full sm:w-auto"
|
128 |
+
onsubmit="event.preventDefault();"
|
129 |
+
>
|
130 |
+
<input
|
131 |
+
type="text"
|
132 |
+
name="name"
|
133 |
+
placeholder="名前を入力"
|
134 |
+
value="${userName}"
|
135 |
+
class="flex-1 px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 bg-gray-700 text-white"
|
136 |
+
/>
|
137 |
+
<button type="button" class="record-button" aria-label="音声録音開始">
|
138 |
+
<div class="record-icon"></div>
|
139 |
+
</button>
|
140 |
+
</form>
|
141 |
+
`;
|
142 |
+
userDiv
|
143 |
+
.querySelector(".record-button")
|
144 |
+
.addEventListener("click", handleRecording);
|
145 |
+
userList.appendChild(userDiv);
|
146 |
+
userCount++;
|
147 |
+
}
|
148 |
+
}
|
149 |
+
|
150 |
+
document.getElementById("add-btn").addEventListener("click", addUser);
|