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:
To stream audio from a SDL app, use the AudioStreamingManager
class. A reference to this class is available from the SdlManager
s 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"); } } }); }
You can also send ByteBuffer
s 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!"); } } });
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 } });