Class AudioContext

java.lang.Object
com.codename1.media.audio.BaseAudioContext
com.codename1.media.audio.AudioContext

public final class AudioContext extends BaseAudioContext

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.

  • 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
      Returns

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

    • getState

      public AudioContextState getState()
      Description copied from class: BaseAudioContext

      The life cycle state.

      Returns

      the state

      Specified by:
      getState in class BaseAudioContext
    • 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.