Display a widget that returns an audio recording from the user's microphone.

Method Signatures and Parameters

Jt.audioInput(String label)

label (String)

A short label explaining to the user what this audio input widget is for. Markdown is supported, see Jt#markdown(String) for more details.

Chainable builder methods

sampleRate(Integer sampleRate)

The target sample rate for the audio recording in Hz. This defaults to 16000 Hz, which is optimal for speech recognition.

The following sample rates are supported: 8000, 11025, 16000, 22050, 24000, 32000, 44100, or 48000. If this is set to null, the widget uses the browser's default sample rate (typically 44100 or 48000 Hz).

help(String help)

A tooltip that gets displayed next to the text. If this is null (default), no tooltip is displayed.

disabled(boolean disabled)

Disable the file uploader if set to true. When disabled, users cannot interact with the widget.

onChange(function.Consumer<JtUploadedFile> onChange)

An optional callback invoked when the file uploader's value changes.

labelVisibility(JtComponent.LabelVisibility labelVisibility)

The visibility of the label. The default is VISIBLE. If this is HIDDEN, Javelit displays an empty spacer instead of the label, which can help keep the widget aligned with other widgets. If this is COLLAPSED, Javelit displays no label or spacer.

width(String width)

The width of the element. This can be one of the following:

  • stretch: The width of the element matches the width of the parent container.
  • An integer specifying the width in pixels: The element has a fixed width. If the specified width is greater than the width of the parent container, the width of the element matches the width of the parent container.

width(int widthPixels)

The width of the text element in pixels. The element will have a fixed width. If the specified width is greater than the width of the parent container, the width of the element matches the width of the parent container.

key(String key)

A string to use as the unique key for the widget. If this is omitted, a key will be generated for the widget based on its content. No two widgets may have the same key.

Only applies to input widgets that have a provided .key(). Disable the persistence of this widget value across runs where the widget does not appear.

use()

Put the widget in the app, in the MAIN container.

use(JtContainer container)

Put the widget in the app, in the provided container.

Returns after .use()

(JtUploadedFile)

The current JtUploadedFile value of the component.

Examples

Record a voice message and play it back. The default sample rate of 16000 Hz is optimal for speech recognition.

import io.javelit.core.Jt;

public class AudioInputApp {
  public static void main(String[] args) {
    var recording = Jt.audioInput("Record a voice message").use();
    if (recording != null) {
      Jt.audio(recording).use();
    }
  }
}
Record high-fidelity audio and play it back. Higher sample rates can create higher-quality, larger audio files. This might require a nicer microphone to fully appreciate the difference.
import io.javelit.core.Jt;

public class HighQualityAudioInputApp {
  public static void main(String[] args) {
    var recording = Jt.audioInput("Record high quality audio").sampleRate(48000).use();
    if (recording != null) {
      Jt.audio(recording).use();
    }
  }
}

forum

Still have questions?

Go to our discussions forum for helpful information and advice from Javelit experts.