Expand Minimize Picture-in-picture Power Device Status Voice Recognition Skip Back Skip Forward Minus Plus Play Search
Internet Explorer alert
This browser is not recommended for use with smartdevicelink.com, and may not function properly. Upgrade to a different browser to guarantee support of all features.
close alert
To Top Created with Sketch. To Top
To Bottom Created with Sketch. To Bottom
Android Guides
Audio Streaming

Audio Streaming

A navigation app can stream raw audio to the head unit. This audio data is played immediately. If audio is already playing, the current audio source will be attenuated and your audio will play. Raw audio must be played with the following parameters:

  • Format: PCM
  • Sample Rate: 16k
  • Number of Channels: 1
  • Bits Per Second (BPS): 16 bits per sample / 2 bytes per sample

To stream audio from a SDL app, use the AudioStreamingManager class. A reference to this class is available from the SdlManagers audioStreamManager property.

Audio Stream Manager

The AudioStreamManager will help you to do on-the-fly transcoding and streaming of your files in mp3 or other formats, or prepare raw PCM data to be queued and played.

Starting the Audio Manager

To stream audio, we call sdlManager.getAudioStreamManager().start() which will start the manager. When that callback returns with a success, call sdlManager.getAudioStreamManager().startAudioStream(). Once this callback returns successfully you can send and play audio.

if (sdlManager.getAudioStreamManager() == null) {
    // Handle the failure
    return;
}

sdlManager.getAudioStreamManager().start(new CompletionListener() {
    @Override
    public void onComplete(boolean success) {
        if (!success) {
            // Failed to start audio streaming manager
            return;
        }
        sdlManager.getAudioStreamManager().startAudioStream(false, new CompletionListener() {
            @Override
            public void onComplete(boolean success) {
                if (!success) {
                    // Failed to start audio stream
                    return;
                }
                // Push Audio Source
            }
        });
    }
});

Playing from File

//Push from Uri Audio Source
sdlManager.getAudioStreamManager().pushAudioSource(audioSourceUri, new CompletionListener() {
    @Override
    public void onComplete(boolean success) {
        if (success) {
            DebugTool.logInfo(TAG, "Audio Uri played successfully!");
        } else {
            DebugTool.logInfo(TAG, "Audio Uri failed to play!");
        }
    }
});

//Push from Raw Audio Source
sdlManager.getAudioStreamManager().pushResource(R.raw.exampleMp3, new CompletionListener() {
    @Override
    public void onComplete(boolean success) {
        if (success) {
            DebugTool.logInfo(TAG, "Audio file played successfully!");
        } else {
            DebugTool.logInfo(TAG, "Audio file failed to play!");
        }
    }
});

Playing from Data

//Push from ByteBuffer Audio Source
sdlManager.getAudioStreamManager().pushBuffer(byteBuffer, new CompletionListener() {
    @Override
    public void onComplete(boolean success) {
        if (success) {
            DebugTool.logInfo(TAG, "Buffer played successfully!");
        } else {
            DebugTool.logInfo(TAG, "Buffer failed to play!");
        }
    }
});

Stopping the Audio Stream

When the stream is complete, or you receive HMI_NONE, you should stop the stream by calling:

sdlManager.getAudioStreamManager().stopAudioStream(new CompletionListener() {
    @Override
    public void onComplete(boolean success) {
        // do something once the stream is stopped
    }
});
View on GitHub.com
Previous Section Next Section