Expand Minimize Picture-in-picture Power Device Status Voice Recognition Skip Back Skip Forward Minus Plus Play Search
Documentation
Sidebar

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.

To stream audio, we call sdlManager.getAudioStreamManager().start() which will start the manager. When that callback returns successful, you call sdlManager.getAudioStreamManager().startAudioStream(). When the callback for that is successful, you can push the audio source using sdlManager.getAudioStreamManager().pushResource(). Below is an example of playing an mp3 file that we have in our resource directory:

if (sdlManager.getAudioStreamManager() != null) {
    Log.i(TAG, "Trying to start audio streaming");
    sdlManager.getAudioStreamManager().start(new CompletionListener() {
        @Override
        public void onComplete(boolean success) {
            if (success) {
                sdlManager.getAudioStreamManager().startAudioStream(false, new CompletionListener() {
                    @Override
                    public void onComplete(boolean success) {
                        if (success) {
                            sdlManager.getAudioStreamManager().pushResource(R.raw.exampleMp3, new CompletionListener() {
                                @Override
                                public void onComplete(boolean success) {
                                    if (success) {
                                        Log.i(TAG, "Audio file played successfully!");
                                    } else {
                                        Log.i(TAG, "Audio file failed to play!");
                                    }
                                }
                            });
                        } else {
                            Log.d(TAG, "Audio stream failed to start!");
                        }
                    }
                });
            } else {
                Log.i(TAG, "Failed to start audio streaming manager");
            }
        }
    });
}

Using a Buffer

You can also send ByteBuffers to the AudioStreamManager to be played. To use it, replace the pushResource call in the example above to the pushBuffer call shown below:

sdlManager.getAudioStreamManager().pushBuffer(byteBuffer, new CompletionListener() {
    @Override
    public void onComplete(boolean success) {
        if (success) {
            Log.i(TAG, "Buffer played successfully!");
        } else {
            Log.i(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