m4a support on search page stream

This commit is contained in:
Broque Thomas 2025-09-22 12:41:05 -07:00
parent 81a77e8d88
commit 8cceef0610
2 changed files with 49 additions and 14 deletions

View file

@ -2961,6 +2961,8 @@ def stream_audio():
'.aac': 'audio/aac', '.aac': 'audio/aac',
'.m4a': 'audio/mp4', '.m4a': 'audio/mp4',
'.wav': 'audio/wav', '.wav': 'audio/wav',
'.opus': 'audio/ogg',
'.webm': 'audio/webm',
'.wma': 'audio/x-ms-wma' '.wma': 'audio/x-ms-wma'
} }

View file

@ -1223,39 +1223,65 @@ function getFileExtension(filename) {
function isAudioFormatSupported(filename) { function isAudioFormatSupported(filename) {
const ext = getFileExtension(filename); const ext = getFileExtension(filename);
const supportedFormats = ['mp3', 'ogg', 'wav']; // Most reliable formats const supportedFormats = ['mp3', 'ogg', 'wav']; // Most reliable formats
const partialSupport = ['flac', 'aac']; // Depends on browser const partialSupport = ['flac', 'aac', 'm4a', 'opus', 'webm']; // Test browser support
const unsupported = ['m4a', 'wma', 'ape', 'aiff']; // Generally problematic const unsupported = ['wma', 'ape', 'aiff']; // Generally problematic
if (supportedFormats.includes(ext)) { if (supportedFormats.includes(ext)) {
return true; return true;
} }
if (partialSupport.includes(ext)) { if (partialSupport.includes(ext)) {
// Test if browser can actually play this format // Test if browser can actually play this format
return canPlayAudioFormat(ext); return canPlayAudioFormat(ext);
} }
return false; // Unsupported formats return false; // Unsupported formats
} }
function canPlayAudioFormat(extension) { function canPlayAudioFormat(extension) {
const audio = document.createElement('audio'); const audio = document.createElement('audio');
const mimeTypes = { const mimeTypes = {
'mp3': 'audio/mpeg', 'mp3': 'audio/mpeg',
'ogg': 'audio/ogg; codecs="vorbis"', 'ogg': 'audio/ogg; codecs="vorbis"',
'wav': 'audio/wav', 'wav': 'audio/wav',
'flac': 'audio/flac', 'flac': 'audio/flac',
'aac': 'audio/aac', 'aac': 'audio/aac',
'm4a': 'audio/mp4', 'm4a': 'audio/mp4; codecs="mp4a.40.2"', // More specific M4A MIME type
'opus': 'audio/ogg; codecs="opus"',
'webm': 'audio/webm; codecs="opus"',
'wma': 'audio/x-ms-wma' 'wma': 'audio/x-ms-wma'
}; };
const mimeType = mimeTypes[extension]; const mimeType = mimeTypes[extension];
if (!mimeType) return false; if (!mimeType) {
console.warn(`🎵 [FORMAT CHECK] No MIME type found for extension: ${extension}`);
return false;
}
const canPlay = audio.canPlayType(mimeType); const canPlay = audio.canPlayType(mimeType);
return canPlay === 'probably' || canPlay === 'maybe'; console.log(`🎵 [FORMAT CHECK] ${extension} (${mimeType}): ${canPlay}`);
let isSupported = canPlay === 'probably' || canPlay === 'maybe';
// Special handling for M4A - try fallback MIME types if first one fails
if (!isSupported && extension === 'm4a') {
const fallbackMimeTypes = ['audio/mp4', 'audio/x-m4a', 'audio/aac'];
console.log(`🎵 [FORMAT CHECK] M4A failed with primary MIME type, trying fallbacks...`);
for (const fallbackMime of fallbackMimeTypes) {
const fallbackResult = audio.canPlayType(fallbackMime);
console.log(`🎵 [FORMAT CHECK] M4A fallback (${fallbackMime}): ${fallbackResult}`);
if (fallbackResult === 'probably' || fallbackResult === 'maybe') {
isSupported = true;
console.log(`🎵 [FORMAT CHECK] M4A supported with fallback MIME type: ${fallbackMime}`);
break;
}
}
}
console.log(`🎵 [FORMAT CHECK] ${extension} final support result: ${isSupported}`);
return isSupported;
} }
// =============================== // ===============================
@ -5461,10 +5487,17 @@ async function streamTrack(index) {
console.log(`🎵 Streaming track:`, result); console.log(`🎵 Streaming track:`, result);
// Check for unsupported formats before streaming // Check for unsupported formats before streaming
if (result.filename && !isAudioFormatSupported(result.filename)) { if (result.filename) {
const format = getFileExtension(result.filename); const format = getFileExtension(result.filename);
showToast(`Sorry, ${format.toUpperCase()} format is not supported in web browsers. Try downloading instead.`, 'error'); console.log(`🎵 [STREAM CHECK] File: ${result.filename}, Extension: ${format}`);
return;
const isSupported = isAudioFormatSupported(result.filename);
console.log(`🎵 [STREAM CHECK] Format ${format} supported: ${isSupported}`);
if (!isSupported) {
showToast(`Sorry, ${format.toUpperCase()} format is not supported in your browser. Try downloading instead.`, 'error');
return;
}
} }
await startStream(result); await startStream(result);