-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdebug_offline_audio.html
More file actions
346 lines (296 loc) · 13.5 KB
/
Copy pathdebug_offline_audio.html
File metadata and controls
346 lines (296 loc) · 13.5 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Debug Offline Audio</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background: #1a1a1a;
color: white;
}
.test-container {
max-width: 800px;
margin: 0 auto;
}
button {
padding: 10px 20px;
margin: 10px;
background: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background: #0056b3;
}
.log {
background: #2d2d2d;
padding: 15px;
border-radius: 5px;
margin: 10px 0;
white-space: pre-wrap;
max-height: 400px;
overflow-y: auto;
}
audio {
width: 100%;
margin: 10px 0;
}
.status {
padding: 10px;
margin: 10px 0;
border-radius: 5px;
}
.success { background: #28a745; }
.error { background: #dc3545; }
.warning { background: #ffc107; color: black; }
</style>
</head>
<body>
<div class="test-container">
<h1>MelodyMind Offline Audio Debug Tool</h1>
<div class="status" id="status">Loading...</div>
<h2>Test Controls</h2>
<button onclick="testIndexedDB()">Test IndexedDB</button>
<button onclick="listCachedSongs()">List Cached Songs</button>
<button onclick="testBlobCreation()">Test Blob Creation</button>
<button onclick="clearCache()">Clear Cache</button>
<h2>Audio Test</h2>
<audio id="testAudio" controls></audio>
<button onclick="testOfflineAudio()">Test Offline Audio</button>
<h2>Debug Log</h2>
<div class="log" id="log"></div>
</div>
<script>
let logElement = document.getElementById('log');
let statusElement = document.getElementById('status');
let audioElement = document.getElementById('testAudio');
function log(message, type = 'info') {
const timestamp = new Date().toLocaleTimeString();
const formattedMessage = `[${timestamp}] ${message}\n`;
logElement.textContent += formattedMessage;
logElement.scrollTop = logElement.scrollHeight;
console.log(message);
}
function setStatus(message, type = 'info') {
statusElement.textContent = message;
statusElement.className = `status ${type}`;
}
// Initialize IndexedDB
async function initializeIndexedDB() {
return new Promise((resolve, reject) => {
const request = indexedDB.open('MelodyMindOfflineDB', 1);
request.onupgradeneeded = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains('audioFiles')) {
db.createObjectStore('audioFiles', { keyPath: 'url' });
}
};
request.onsuccess = (event) => {
resolve(event.target.result);
};
request.onerror = (event) => {
reject(event.target.error);
};
});
}
// Test IndexedDB connection
// Utility function to ensure HTTPS URLs
function ensureHttpsUrl(url) {
if (window.location.protocol === 'https:' && url.startsWith('http:')) {
return url.replace('http:', 'https:');
}
return url;
}
async function testIndexedDB() {
try {
log('Testing IndexedDB connection...');
const db = await initializeIndexedDB();
log('✅ IndexedDB connected successfully');
setStatus('IndexedDB working', 'success');
} catch (error) {
log(`❌ IndexedDB error: ${error.message}`);
setStatus('IndexedDB failed', 'error');
}
}
// List cached songs
async function listCachedSongs() {
try {
log('Listing cached songs...');
const db = await initializeIndexedDB();
const transaction = db.transaction(['audioFiles'], 'readonly');
const store = transaction.objectStore('audioFiles');
const request = store.getAll();
request.onsuccess = (event) => {
const cachedSongs = event.target.result;
log(`Found ${cachedSongs.length} cached songs:`);
cachedSongs.forEach((song, index) => {
log(`${index + 1}. URL: ${song.url.substring(0, 50)}...`);
log(` Blob size: ${(song.blob.size / 1024 / 1024).toFixed(2)} MB`);
log(` Blob type: ${song.blob.type}`);
log(` Cached: ${new Date(song.timestamp).toLocaleString()}`);
log('');
});
if (cachedSongs.length === 0) {
setStatus('No cached songs found', 'warning');
} else {
setStatus(`${cachedSongs.length} songs cached`, 'success');
}
};
request.onerror = (event) => {
log(`❌ Error listing songs: ${event.target.error}`);
setStatus('Failed to list songs', 'error');
};
} catch (error) {
log(`❌ Error: ${error.message}`);
setStatus('List failed', 'error');
}
}
// Test blob creation and audio playback
async function testBlobCreation() {
try {
log('Testing blob creation...');
const db = await initializeIndexedDB();
const transaction = db.transaction(['audioFiles'], 'readonly');
const store = transaction.objectStore('audioFiles');
const request = store.getAll();
request.onsuccess = async (event) => {
const cachedSongs = event.target.result;
if (cachedSongs.length === 0) {
log('❌ No cached songs to test');
setStatus('No songs to test', 'warning');
return;
}
const firstSong = cachedSongs[0];
log(`Testing blob creation for: ${firstSong.url.substring(0, 50)}...`);
try {
// Create blob URL
const blobUrl = URL.createObjectURL(firstSong.blob);
log(`✅ Blob URL created: ${blobUrl}`);
// Test fetch on blob URL
const response = await fetch(blobUrl, { method: 'HEAD' });
log(`✅ Blob fetch test: ${response.ok ? 'Success' : 'Failed'}`);
log(` Status: ${response.status}`);
log(` Content-Type: ${response.headers.get('content-type')}`);
log(` Content-Length: ${response.headers.get('content-length')}`);
// Test audio element
const testAudio = new Audio();
testAudio.src = blobUrl;
testAudio.preload = 'metadata';
await new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error('Audio metadata load timeout'));
}, 10000);
testAudio.addEventListener('loadedmetadata', () => {
clearTimeout(timeout);
log(`✅ Audio metadata loaded successfully`);
log(` Duration: ${testAudio.duration} seconds`);
log(` Ready state: ${testAudio.readyState}`);
resolve();
});
testAudio.addEventListener('error', (e) => {
clearTimeout(timeout);
log(`❌ Audio load error: ${e.message || 'Unknown error'}`);
reject(e);
});
});
// Set to main audio element for testing
audioElement.src = blobUrl;
setStatus('Blob test successful', 'success');
// Clean up
setTimeout(() => {
URL.revokeObjectURL(blobUrl);
log('🔄 Blob URL cleaned up');
}, 60000); // Clean up after 1 minute
} catch (blobError) {
log(`❌ Blob test failed: ${blobError.message}`);
setStatus('Blob test failed', 'error');
}
};
request.onerror = (event) => {
log(`❌ Error accessing songs: ${event.target.error}`);
setStatus('Access failed', 'error');
};
} catch (error) {
log(`❌ Error: ${error.message}`);
setStatus('Test failed', 'error');
}
}
// Test offline audio playback
async function testOfflineAudio() {
try {
log('Testing offline audio playback...');
if (!audioElement.src) {
log('❌ No audio source set. Run "Test Blob Creation" first.');
setStatus('No audio source', 'warning');
return;
}
// Attempt to play
audioElement.currentTime = 0;
const playPromise = audioElement.play();
if (playPromise !== undefined) {
playPromise.then(() => {
log('✅ Audio playback started successfully');
setStatus('Audio playing', 'success');
// Pause after 3 seconds
setTimeout(() => {
audioElement.pause();
log('🔄 Audio paused after test');
}, 3000);
}).catch((error) => {
log(`❌ Audio playback failed: ${error.message}`);
setStatus('Playback failed', 'error');
});
}
} catch (error) {
log(`❌ Error: ${error.message}`);
setStatus('Test failed', 'error');
}
}
// Clear cache
async function clearCache() {
try {
log('Clearing audio cache...');
const db = await initializeIndexedDB();
const transaction = db.transaction(['audioFiles'], 'readwrite');
const store = transaction.objectStore('audioFiles');
const request = store.clear();
request.onsuccess = () => {
log('✅ Cache cleared successfully');
setStatus('Cache cleared', 'success');
};
request.onerror = (event) => {
log(`❌ Error clearing cache: ${event.target.error}`);
setStatus('Clear failed', 'error');
};
} catch (error) {
log(`❌ Error: ${error.message}`);
setStatus('Clear failed', 'error');
}
}
// Initialize on page load
window.addEventListener('load', async () => {
log('🚀 Debug tool initialized');
await testIndexedDB();
await listCachedSongs();
});
// Audio event listeners
audioElement.addEventListener('loadstart', () => log('🎵 Audio load started'));
audioElement.addEventListener('loadeddata', () => log('✅ Audio data loaded'));
audioElement.addEventListener('canplay', () => log('✅ Audio can play'));
audioElement.addEventListener('play', () => log('▶️ Audio play event'));
audioElement.addEventListener('pause', () => log('⏸️ Audio pause event'));
audioElement.addEventListener('error', (e) => {
log(`❌ Audio error: ${e.message || 'Unknown error'}`);
if (e.target && e.target.error) {
log(` Error code: ${e.target.error.code}`);
log(` Error message: ${e.target.error.message}`);
}
});
</script>
</body>
</html>