as

Settings
Sign out
Notifications
Alexa
Amazon Appstore
AWS
Documentation
Support
Contact Us
My Cases
Get Started
Design and Develop
Publish
Reference
Support

@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

  1. Add the following library dependency to the dependencies section of your package.json file.

    Copied to clipboard.

     "@amazon-devices/keplerscript-audio-lib": "~2.0.0"
    
  2. Add the following privilege and services request in your manifest.toml.

    Copied to clipboard.

    [[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.

Copied to clipboard.

import { 
    AudioManager,
    AudioPlaybackStream,
    AudioPlaybackStreamBuilder,
    AudioFocusManager,
    AudioFocusSession
} from '@amazon-devices/keplerscript-audio-lib';

Audio

Manage Audio Volume

Copied to clipboard.

// 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

Copied to clipboard.

// 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

Copied to clipboard.

// 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

Copied to clipboard.

// 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

Copied to clipboard.

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

Copied to clipboard.

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

  1. Permission issues:
    1. Error: No Permission exception
    2. Solution: Ensure required privileges are properly declared in your manifest.toml.
  2. AudioManager not created:
    1. Error: getAudioManager() returns undefined
    2. Solution: Ensure that the KeplerAudioInterface Turbo Module is properly set up and supported in your app environment.
  3. Invalid Observer or callback:
    1. Error: "not invoking Callback"
    2. Solution: Ensure that you are correctly subscribing to the Observers.
  4. Unsubscription failures:
    1. Error: Unsubscribe rejection
    2. Solution: Check that you're using the correct observer object and that it hasn't already been used to unsubscribe.

Modules


Last updated: Sep 30, 2025