Skip to content

Commit

Permalink
support more audio file types
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmoudz committed Apr 21, 2024
1 parent 641415b commit 7c29e7b
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/core/AudioRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import Logger from './Logger';

class AudioRecorder {
// Stop recording after x seconds as hard limit
private maxRecordingTime = 10000;
private maxRecordingTime = 7000;
// Stop recording after x seconds of silence
private silenceThreshold = 1500;
private silenceThreshold = 1000;
private mediaRecorder: MediaRecorder | null = null;
private stream: MediaStream | null = null;
private audioChunks: Blob[] = [];
Expand Down Expand Up @@ -67,7 +67,21 @@ class AudioRecorder {

public async startRecording(): Promise<Blob> {
const stream = await this.getMediaStream();
this.mediaRecorder = new MediaRecorder(stream);
let options = { mimeType: 'audio/webm;codecs=opus' };
if (!MediaRecorder.isTypeSupported(options.mimeType)) {
if (MediaRecorder.isTypeSupported('audio/mp4')) {
options.mimeType = 'audio/mp4';
} else if (
MediaRecorder.isTypeSupported('audio/ogg; codecs=opus')
) {
options.mimeType = 'audio/ogg; codecs=opus';
} else {
console.error('No supported audio type found');
throw new Error('No supported audio type found');
}
}

this.mediaRecorder = new MediaRecorder(stream, options);
this.mediaRecorder.ondataavailable = this.handleDataAvailable;
this.mediaRecorder.onstop = this.handleStop;
this.mediaRecorder.start();
Expand Down

0 comments on commit 7c29e7b

Please sign in to comment.