@amazon-devices/keplerscript-audio-lib
The Kepler Audio Interface API provides functionality for managing audio in apps. It consists of several key components:
- Audio Manager: Handles system-level audio controls like volume, mute, and device management.
- Audio Playback: Enables audio stream playback with configurable attributes.
- Audio Focus Management: Manages audio focus for different audio streams.
The API is designed to work with React Native for Kepler apps and provides a foundation for building audio-enabled applications with features such as:
- Volume and mute control for different audio types
- Audio device management
- System sound management
- Audio focus handling
- Audio stream configuration and management
Get started
Setup
-
Add the following library dependency to the
dependencies
section of your package.json file."@amazon-devices/keplerscript-audio-lib": "~2.0.0"
-
Add the following privilege and services request in your manifest.toml.
[[needs.privilege]] id = "com.amazon.audio.privilege.microphone.access" [wants] // only add the services that are needed [[wants.service]] id = "com.amazon.audio.stream" // For performing audio playback with the AudioStream API. [[wants.service]] id = "com.amazon.audio.control" // For AudioManager, AudioFocus APIs usage. [[wants.service]] id = "com.amazon.audio.system" // For playing brief sounds with low latency using SoundPlayer or SystemSound APIs.
For more information on the needs section of the manifest file, see Manifest needs
Section.
Usage
Import the KeplerAudioInterface Turbo module into your project.
import {
AudioManager,
AudioPlaybackStream,
AudioPlaybackStreamBuilder,
AudioFocusManager,
AudioFocusSession
} from '@amazon-devices/keplerscript-audio-lib';
Audio
Manage Audio Volume
// Set volume for media
await AudioManager.setVolumeAsync(AudioVolumeType.VOLUME_TYPE_MEDIA, 75, AudioVolumeFlags.VOLUME_FLAG_SHOW_UI);
// Get current volume
const volume = await AudioManager.getVolumeAsync(AudioVolumeType.VOLUME_TYPE_MEDIA);
Create and Manage audio playback
// Create a playback stream
const builder = new AudioPlaybackStreamBuilder();
builder.setAudioConfig({
sampleRate: AudioSampleRate.SAMPLE_RATE_44_1_KHZ,
channelMask: AudioChannelMask.CHANNEL_STEREO,
format: AudioSampleFormat.FORMAT_PCM_16_BIT
});
builder.setAudioAttributes({
contentType: AudioContentType.CONTENT_TYPE_MUSIC,
usage: AudioUsageType.USAGE_MEDIA,
flags: AudioFlags.FLAG_NONE
});
const playbackStream = await builder.buildAsync();
await playbackStream.startAsync();
Managing audio focus
// Create and request audio focus
const focusSession = await AudioFocusManager.createAudioFocusSessionAsync();
await focusSession.requestAudioFocusAsync({
usage: AudioUsageType.USAGE_MEDIA
});
// Register for focus changes
await focusSession.registerAudioFocusListenerAsync((focusChange) => {
console.log('Focus changed:', focusChange);
});
Event Observers
Audio Manager events
// Register for system-wide audio events
await AudioManager.registerAudioEventObserverAsync((event) => {
// Event object will contain different properties based on the event type
switch (event.audioEvent) {
case AudioEvent.DEVICE_STATE_UPDATE:
// Triggered when an audio device's connection state changes
// Parameters:
// - device: AudioDevice enum value
// - role: AudioRole enum value
// - connect: boolean indicating if device was connected (true) or disconnected (false)
console.log(`Device ${event.device} with role ${event.role} ${event.connect ? 'connected' : 'disconnected'}`);
break;
case AudioEvent.VOLUME_UPDATE:
// Triggered when volume changes for any audio type
// Parameters:
// - type: AudioVolumeType enum value
// - volume: number (0-100)
console.log('Volume changed for:', event.type, 'to:', event.volume);
break;
case AudioEvent.GLOBAL_VOLUME_MUTE_UPDATE:
// Triggered when global mute state changes
// Parameters:
// - mute: boolean indicating if system is muted
console.log('Global mute updated to state: ', event.mute);
break;
case AudioEvent.SERVER_DOWN:
// Triggered when audio server becomes unavailable
console.log('Audio server is down');
break;
case AudioEvent.SERVER_UP:
// Triggered when audio server becomes available
console.log('Audio server is up');
break;
case AudioEvent.AUDIO_USAGE_STATE_CHANGE:
// Triggered when audio usage state changes
// Parameters:
// - usage: AudioUsageType enum value
// - active: boolean indicating if usage became active
console.log('Usage: ', event.usage);
console.log('Active status: ', event.active);
break;
case AudioEvent.MIC_MUTE_STATE_UPDATE:
// Triggered when microphone mute state changes
// Parameters:
// - state: boolean indicating if mic is muted
console.log('Mic mute state changed: ', event.muteState);
break;
case AudioEvent.VOLUME_MUTE_UPDATE:
// Triggered when volume mute state changes for specific volume type
// Parameters:
// - volumeType: AudioVolumeType enum value
// - mute: boolean indicating if type is muted
console.log('Volume type: ', event.volumeType);
console.log('Mute state: ', event.muteState);
break;
case AudioEvent.TELEPHONY_MUTE_UPDATE:
// Triggered when telephony mute state changes
// Parameters:
// - deviceType: AudioDevice enum value
// - address: string representing device address
// - deviceName: string representing device name
// - mute: boolean indicating if telephony is muted
console.log('Telephony mute update!: ');
console.log('Device type: ', event.deviceType);
console.log('Device address: ', event.deviceAddress);
console.log('Device name: ', event.deviceName);
console.log('Device mute state: ', event.muteState);
break;
}
});
// Cleanup
await AudioManager.unregisterAudioEventObserverAsync();
Playback a stream
const playbackStream = await builder.buildAsync();
await playbackStream.registerEventObserverAsync((event) => {
switch (event.type) {
case AudioPlaybackEvent.DIED:
// Triggered when the playback stream becomes unavailable
break;
case AudioPlaybackEvent.RECOVERED:
// Triggered when the playback stream recovers from a died state
break;
case AudioPlaybackEvent.STOPPED:
// Triggered when the playback stream stops
break;
case AudioPlaybackEvent.MUTE_STATE_UPDATE:
// Triggered when the stream's mute state changes
// Parameters:
// - muted: boolean indicating if stream is muted
console.log('MuteState: ', event.muteState);
break;
case AudioPlaybackEvent.FRAME_UNDERRUN:
// Triggered when a buffer underrun occurs
// Parameters:
// - size: number indicating the underrun size in frames
console.log('Underrun count: ', event.underrunCount);
break;
}
});
// Cleanup
await playbackStream.unregisterEventObserverAsync();
Audio focus
const focusSession = await AudioFocusManager.createAudioFocusSessionAsync();
await focusSession.registerAudioFocusListenerAsync((focusChange) => {
switch (focusChange.type) {
case AudioFocusChange.GRANTED:
// Triggered when audio focus is granted
break;
case AudioFocusChange.RELEASED:
// Triggered when audio focus is released
break;
case AudioFocusChange.DUCKED:
// Triggered when audio should be ducked (reduced in volume)
break;
case AudioFocusChange.PAUSED:
// Triggered when audio should be paused
break;
case AudioFocusChange.STOPPED:
// Triggered when audio should be stopped
break;
}
});
// Cleanup
await focusSession.unregisterAudioFocusListenerAsync();
Troubleshooting
- Permission issues:
- Error: No Permission exception
- Solution: Ensure required privileges are properly declared in your manifest.toml.
- AudioManager not created:
- Error:
getAudioManager()
returns undefined - Solution: Ensure that the KeplerAudioInterface Turbo Module is properly set up and supported in your app environment.
- Error:
- Invalid Observer or callback:
- Error: "not invoking Callback"
- Solution: Ensure that you are correctly subscribing to the Observers.
- Unsubscription failures:
- Error: Unsubscribe rejection
- Solution: Check that you're using the correct observer object and that it hasn't already been used to unsubscribe.
Modules
- AudioFocusClientTurboModule
- AudioFocusManager
- AudioFocusSession
- AudioManager
- AudioManagerClientTurboModule
- AudioPlaybackStream
- AudioPlaybackStreamBuilder
- AudioPlaybackStreamTurboModule
- AudioRecordStream
- AudioRecordStreamBuilder
- AudioRecordStreamTurboModule
- types/AudioCoreClientTypes
- types/AudioFocusClientTypes
Last updated: Sep 30, 2025