197 lines
6.9 KiB
JavaScript
197 lines
6.9 KiB
JavaScript
/**
|
|
* 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()'); |