Skip to content

Commit

Permalink
Determine the correct audio format for recording based on the current…
Browse files Browse the repository at this point in the history
… running operating system
  • Loading branch information
besidev committed Jan 12, 2024
1 parent 01dc658 commit 5d2377e
Showing 1 changed file with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public NativeMediaRecorder() {

// Initialize audio frame grabber
micGrabber = new FFmpegFrameGrabber(getDefaultAudioInputDevice());
micGrabber.setFormat("avfoundation");
micGrabber.setFormat(getDefaultAudioInputFormat());
micGrabber.setAudioChannels(DEFAULT_AUDIO_CHANNELS);
micGrabber.setSampleRate(DEFAULT_AUDIO_SAMPLE_RATE);

Expand Down Expand Up @@ -592,4 +592,41 @@ public static String getDefaultAudioInputDevice() {
return audioDevice;
}

/**
* Gets the default audio input format based on the operating system.
* <p>
* This method determines the appropriate audio input format for use with {@link FFmpegFrameGrabber},
* based on the operating system where the application is running:
* </p>
* <ul>
* <li>Windows: Returns "dshow" for DirectShow.</li>
* <li>macOS: Returns "avfoundation" for AVFoundation.</li>
* <li>Linux: Returns "alsa" for ALSA.</li>
* <li>Other: Returns "default" as a fallback,
* but this should be adjusted based on specific requirements or environment.</li>
* </ul>
*
* @return the format string for audio input format
*/
public static String getDefaultAudioInputFormat() {
String OS = System.getProperty("os.name").toLowerCase();
String format;

if (OS.contains("win")) {
// Windows - DirectShow
format = "dshow";
} else if (OS.contains("mac")) {
// macOS - AVFoundation
format = "avfoundation";
} else if (OS.contains("nix") || OS.contains("nux") || OS.contains("aix")) {
// Linux - ALSA
format = "alsa";
} else {
// Unknown OS - fallback
format = "default";
}

return format;
}

}

0 comments on commit 5d2377e

Please sign in to comment.