Display an audio player.

Javelit attempts to infer the format (MIME type) from the input. If format inference fails, passing the format directly with .format() is necessary.

Method Signatures and Parameters

Jt.audio(String url)

url (String)

A URL for a hosted audio file.

Jt.audio(byte[] data)

data (byte[])

Raw audio data.

Jt.audio(java.nio.file.Path filePath)

filePath (java.nio.file.Path)

A path to a local audio file. The path can be absolute or relative to the working directory.

Jt.audio(JtUploadedFile uploadedFile)

uploadedFile (io.javelit.core.JtUploadedFile)

An uploaded file.

Chainable builder methods

format(String format)

The MIME type for the audio file. For URLs, this defaults to "audio/wav". For files and raw data, Javelit attempts to infer the MIME type. For more information about MIME types, see https://www.iana.org/assignments/media-types/media-types.xhtml.

startTime(java.time.Duration startTime)

The time from which the element should start playing as a java.time.Duration, rounded to the milliseconds. If null (default), the element plays from the beginning.

startTime(int seconds)

The time from which the element should start playing in seconds. By default, the element plays from the beginning.

endTime(java.time.Duration endTime)

The time at which the element should stop playing as a java.time.Duration, rounded to the milliseconds. If null (default), the element plays through to the end.

endTime(int seconds)

The time at which the element should stop playing in seconds. By default, the element plays through to the end.

loop(boolean loop)

Whether the audio should loop playback. False by default.

autoplay(boolean autoplay)

Whether the audio file should start playing automatically. This is False by default. Browsers will not autoplay audio files if the user has not interacted with the page by clicking somewhere.

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.

Examples

Audio from external URL

import io.javelit.core.Jt;

public class UrlAudioApp {
  public static void main(String[] args) {
    Jt.audio("https://github.com/javelit/public_assets/raw/refs/heads/main/audio/piano-chords.mp3").use();
  }
}
Audio from static resource
package staticUrlAudioApp;

import io.javelit.core.Jt;

public class StaticUrlAudioApp {
  public static void main(String[] args) {
    // static/piano-chords.mp3 is present in the working directory
    Jt.audio("app/static/piano-chords.mp3").use();
  }
}

Audio from raw data

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

import io.javelit.core.Jt;

public class RawDataAudioApp {
  public static void main(String[] args) {
    byte[] beepWav = generateBeepWavBytes(2);
    Jt.audio(beepWav).format("audio/wav").use();
  }

  private static byte[] generateBeepWavBytes(int seconds) {
    final float sampleRate = 44100;
    final int numSamples = (int) (seconds * sampleRate);
    final int numChannels = 1;  // mono
    byte[] data = new byte[numSamples];
    // Generate simple square wave beep at 440 Hz
    int period = (int) (sampleRate / 440); // ~440 Hz
    for (int i = 0; i < numSamples; i++) {
      data[i] = i % period < period / 2 ? (byte) 127 : (byte) -128;
    }
    // Wrap raw PCM data into WAV format
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    AudioFormat format = new AudioFormat(sampleRate, 8, // bits per sample
                                         numChannels, true, // signed
                                         false // little endian
    );

    try (AudioInputStream ais = new AudioInputStream(new ByteArrayInputStream(data), format, numSamples)) {
      AudioSystem.write(ais, AudioFileFormat.Type.WAVE, baos);
      return baos.toByteArray();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}

Audio from local file

import java.nio.file.Path;

import io.javelit.core.Jt;

public class FileAudioApp {
  public static void main(String[] args) {
    // assumes piano-chords.mp3 is present in the working directory
    Jt.audio(Path.of("piano-chords.mp3")).use();
  }
}

forum

Still have questions?

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