Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class AudioContext
- Object
- BaseAudioContext
- 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
From BaseAudioContext
getSampleRate, getCurrentTime, getDestination, getListener, createWaveShaper, createAnalyser, createDynamicsCompressor, createConvolver, createPanner, createAudioProcessor, createAudioProcessor, isAudioProcessorSupported, createMicrophoneSource, isMicrophoneSourceSupported, decodeAudioData, isHrtfSupported, createBuffer, createGain, createDelay, createDelay, createBufferSource, createConstantSource, createOscillator, createBiquadFilter, createIIRFilter, createStereoPanner, createChannelSplitter, createChannelSplitter, createChannelMerger, createChannelMerger, createPeriodicWave, createPeriodicWave
Method details
isSupported
public static boolean isSupported()Returns
create() can succeedcreate
public static AudioContext create()Returns
create
public static AudioContext create(int sampleRate)Parameters
sampleRateint- the rate wanted, or 0 for the device’s own; the device
may run at another, which
getSampleRate()reports
Returns
getState
public AudioContextState getState()Returns
getBaseLatency
public double getBaseLatency()Returns
getOutputLatency
public double getOutputLatency()Returns
suspend
public void suspend()resume
public void resume()suspend().close
public void close()