Class 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.
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Releases the output for good.static AudioContextcreate()Opens the default output at the device's preferred sample rate.static AudioContextcreate(int sampleRate) Opens the default output.doubleThe processing latency the graph adds: one render quantum.doubleThe latency the platform reports between rendering and hearing a sample.getState()The life cycle state.static booleanWhether this platform can play an audio graph in real time.voidresume()Resumes rendering aftersuspend().voidsuspend()Pauses rendering; the clock stops.Methods inherited from class BaseAudioContext
createAnalyser, createAudioProcessor, createAudioProcessor, createBiquadFilter, createBuffer, createBufferSource, createChannelMerger, createChannelMerger, createChannelSplitter, createChannelSplitter, createConstantSource, createConvolver, createDelay, createDelay, createDynamicsCompressor, createGain, createIIRFilter, createMicrophoneSource, createOscillator, createPanner, createPeriodicWave, createPeriodicWave, createStereoPanner, createWaveShaper, decodeAudioData, getCurrentTime, getDestination, getListener, getSampleRate, isAudioProcessorSupported, isHrtfSupported, isMicrophoneSourceSupported
-
Method Details
-
isSupported
public static boolean isSupported() -
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
Opens the default output.
Parameters
sampleRate: the rate wanted, or 0 for the device's own; the device may run at another, whichBaseAudioContext.getSampleRate()reports
Returns
a running context, or null when the platform has no real-time output
-
getState
Description copied from class:BaseAudioContextThe life cycle state.
Returns
the state
- Specified by:
getStatein classBaseAudioContext
-
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 aftersuspend(). -
close
public void close()Releases the output for good.
-