Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class AudioContext

  1. Object
  2. BaseAudioContext
  3. AudioContext

Renders an audio graph to the device’s speaker in real time, the Web Audio AudioContext.

Not every platform has a real-time audio output; check isSupported() or test create() for null, and fall back to Media or SoundPool playback. OfflineAudioContext works everywhere.

AudioContext audio = AudioContext.create();
if (audio != null) {
    OscillatorNode osc = audio.createOscillator();
    GainNode amp = audio.createGain();
    osc.connect(amp).connect(audio.getDestination());
    double now = audio.getCurrentTime();
    amp.getGain().setValueAtTime(0, now).linearRampToValueAtTime(0.5f, now + 0.02)
            .setTargetAtTime(0, now + 0.1, 0.2);
    osc.start(now);
    osc.stop(now + 1.5);
}

The graph renders on the platform’s audio thread. Callbacks such as AudioScheduledSourceNode.setOnEnded(Runnable) are delivered on the EDT.

Methods

public static boolean isSupported()Whether this platform can play an audio graph in real time.
public static AudioContext create()Opens the default output at the device’s preferred sample rate.
public static AudioContext create(int sampleRate)Opens the default output.
public AudioContextState getState()The life cycle state.
public double getBaseLatency()The processing latency the graph adds: one render quantum.
public double getOutputLatency()The latency the platform reports between rendering and hearing a sample.
public void suspend()Pauses rendering; the clock stops.
public void resume()Resumes rendering after suspend().
public void close()Releases the output for good.

Inherited methods

Method details

isSupported

public static boolean isSupported()
Whether this platform can play an audio graph in real time.

Returns

true when create() can succeed

create

public static AudioContext create()
Opens the default output at the device’s preferred sample rate.

Returns

a running context, or null when the platform has no real-time output

create

public static AudioContext create(int sampleRate)
Opens the default output.

Parameters

sampleRate int
the rate wanted, or 0 for the device’s own; the device may run at another, which getSampleRate() reports

Returns

a running context, or null when the platform has no real-time output

getState

public AudioContextState getState()
The life cycle state.

Returns

the state

getBaseLatency

public double getBaseLatency()
The processing latency the graph adds: one render quantum.

Returns

seconds

getOutputLatency

public double getOutputLatency()
The latency the platform reports between rendering and hearing a sample.

Returns

seconds, or 0 when unknown

suspend

public void suspend()
Pauses rendering; the clock stops.

resume

public void resume()
Resumes rendering after suspend().

close

public void close()
Releases the output for good.