/** * Audio Compatibility Checker and Browser Mode Handler * This script helps detect and handle audio limitations in browser vs Electron mode */ class AudioCompatibilityChecker { constructor() { this.isElectron = window.electronAPI !== undefined; this.isFileProtocol = window.location.protocol === 'file:'; this.browserAudioSupported = this.checkBrowserAudioSupport(); } checkBrowserAudioSupport() { // Test if we can create audio elements try { const testAudio = new Audio(); return testAudio !== null; } catch (error) { console.error('Browser does not support Audio API:', error); return false; } } async testSingleAudioFile(path) { return new Promise((resolve) => { const audio = new Audio(); let resolved = false; const cleanup = () => { if (!resolved) { resolved = true; audio.pause(); audio.src = ''; audio.remove(); } }; audio.addEventListener('canplay', () => { if (!resolved) { cleanup(); resolve({ success: true, path, error: null }); } }); audio.addEventListener('error', (e) => { if (!resolved) { const errorInfo = { code: e.target.error?.code, message: e.target.error?.message, networkState: e.target.networkState, readyState: e.target.readyState, src: e.target.src }; cleanup(); resolve({ success: false, path, error: errorInfo }); } }); // Timeout after 3 seconds setTimeout(() => { if (!resolved) { cleanup(); resolve({ success: false, path, error: 'timeout' }); } }, 3000); // Try to load the audio try { audio.src = path; audio.load(); } catch (error) { if (!resolved) { cleanup(); resolve({ success: false, path, error: error.message }); } } }); } async quickAudioTest() { console.log('šŸŽµ Audio Compatibility Test Starting...'); console.log(`šŸŽµ Environment: ${this.isElectron ? 'Electron' : 'Browser'}`); console.log(`šŸŽµ Protocol: ${window.location.protocol}`); console.log(`šŸŽµ Audio API Support: ${this.browserAudioSupported}`); // Test a few known audio files const testFiles = [ 'audio/tasks/teasing/u.mp3', 'audio/rewards/completion/u.mp3' ]; const results = []; for (const file of testFiles) { console.log(`šŸŽµ Testing: ${file}`); const result = await this.testSingleAudioFile(file); results.push(result); if (result.success) { console.log(`āœ… ${file} - Working`); } else { console.log(`āŒ ${file} - Failed:`, result.error); } // Small delay between tests await new Promise(resolve => setTimeout(resolve, 100)); } return results; } getRecommendations() { const recommendations = []; if (!this.isElectron && this.isFileProtocol) { recommendations.push({ type: 'warning', message: 'Running in browser with file:// protocol', suggestion: 'Use "npm start" to run the Electron desktop version for full audio support' }); } if (!this.browserAudioSupported) { recommendations.push({ type: 'error', message: 'Browser does not support HTML5 Audio API', suggestion: 'Try a different browser or update your current browser' }); } if (this.isElectron) { recommendations.push({ type: 'success', message: 'Running in Electron desktop mode', suggestion: 'Audio should work without restrictions' }); } return recommendations; } async diagnoseAudioIssues() { console.log('šŸŽµ Audio Diagnostic Report'); console.log('=' .repeat(50)); const recommendations = this.getRecommendations(); recommendations.forEach(rec => { const icon = rec.type === 'error' ? 'āŒ' : rec.type === 'warning' ? 'āš ļø' : 'āœ…'; console.log(`${icon} ${rec.message}`); console.log(` šŸ’” ${rec.suggestion}`); }); console.log('\nšŸŽµ Testing Audio Files...'); const testResults = await this.quickAudioTest(); const workingFiles = testResults.filter(r => r.success).length; const totalFiles = testResults.length; console.log('\nšŸŽµ Summary:'); console.log(`āœ… Working: ${workingFiles}/${totalFiles}`); console.log(`āŒ Failed: ${totalFiles - workingFiles}/${totalFiles}`); if (workingFiles === 0) { console.log('\nšŸŽµ No audio files are working. This suggests:'); console.log(' - Browser CORS restrictions (use Electron app)'); console.log(' - Audio files are corrupted or missing'); console.log(' - Browser audio support issues'); } else if (workingFiles < totalFiles) { console.log('\nšŸŽµ Some audio files are working. Issues may be:'); console.log(' - Specific file corruption'); console.log(' - Filename/path issues'); console.log(' - Audio format compatibility'); } else { console.log('\nšŸŽµ All tested files are working! šŸŽ‰'); } return { environment: { isElectron: this.isElectron, isFileProtocol: this.isFileProtocol, audioSupported: this.browserAudioSupported }, testResults, recommendations }; } } // Auto-run diagnostic if audio issues are detected window.audioCompatibilityChecker = new AudioCompatibilityChecker(); // Quick check for common issues if (!window.audioCompatibilityChecker.isElectron && window.audioCompatibilityChecker.isFileProtocol) { console.log('šŸŽµ Audio issues detected! Run window.audioCompatibilityChecker.diagnoseAudioIssues() for full report'); } console.log('šŸŽµ Audio Compatibility Checker loaded'); console.log('šŸŽµ Run: window.audioCompatibilityChecker.diagnoseAudioIssues()');