Skip to content

Commit aac1007

Browse files
committed
fix: improve Safari audio compatibility and error handling
- Prioritize Safari-compatible audio formats (MP4/AAC, MPEG, WAV) - Add dynamic file extension selection based on supported format - Improve error logging for both recording and playback - Add comprehensive error details for audio playback issues
1 parent fdc267a commit aac1007

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

app/src/components/Post.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,21 @@ export default function Post({ post }: Properties): ReactElement {
4545
)}
4646
{post.audio && (
4747
<div className="mt-3">
48-
<audio controls src={`${BACKEND_HOST}${post.audio}`} className="w-full" />
48+
<audio
49+
controls
50+
src={`${BACKEND_HOST}${post.audio}`}
51+
className="w-full"
52+
onError={(e) => {
53+
const audioElement = e.currentTarget;
54+
console.error('Audio playback error:', {
55+
event: e,
56+
src: audioElement.src,
57+
networkState: audioElement.networkState,
58+
readyState: audioElement.readyState,
59+
errorCode: audioElement.error
60+
});
61+
}}
62+
/>
4963
</div>
5064
)}
5165
</div>

app/src/components/PostForm.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,24 @@ export default function PostForm(): ReactElement {
4242
try {
4343
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
4444

45-
// Check for supported MIME types
45+
// Check for supported MIME types, prioritizing Safari-compatible formats
4646
const mimeType = [
47+
'audio/mp4',
48+
'audio/mp4;codecs=mp4a',
49+
'audio/mpeg',
50+
'audio/wav',
4751
'audio/webm',
4852
'audio/webm;codecs=opus',
49-
'audio/ogg;codecs=opus',
50-
'audio/mp4'
51-
].find(type => MediaRecorder.isTypeSupported(type)) || '';
53+
'audio/ogg;codecs=opus'
54+
].find(type => {
55+
const isSupported = MediaRecorder.isTypeSupported(type);
56+
console.log(`MIME type ${type} supported: ${isSupported}`);
57+
return isSupported;
58+
}) || '';
59+
60+
if (!mimeType) {
61+
console.warn('No supported MIME types found for audio recording');
62+
}
5263

5364
const recorder = new MediaRecorder(stream, mimeType ? { mimeType } : undefined);
5465
const chunks: BlobPart[] = [];
@@ -60,8 +71,11 @@ export default function PostForm(): ReactElement {
6071
};
6172

6273
recorder.onstop = () => {
63-
const blob = new Blob(chunks, { type: mimeType || 'audio/webm' });
64-
const file = new File([blob], 'recording.webm', { type: mimeType || 'audio/webm' });
74+
const blob = new Blob(chunks, { type: mimeType || 'audio/mp4' });
75+
const fileExtension = mimeType.includes('mp4') ? 'mp4' :
76+
mimeType.includes('webm') ? 'webm' :
77+
mimeType.includes('wav') ? 'wav' : 'mp4';
78+
const file = new File([blob], `recording.${fileExtension}`, { type: mimeType || 'audio/mp4' });
6579
setForm(state => ({ ...state, audio: file }));
6680

6781
// Clean up old preview URL and create new one

0 commit comments

Comments
 (0)