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 SDLStreamingMediaManager class. A reference to this class is available from the SDLManager's streamManager property.

Audio Stream Lifecycle

Like the lifecycle of the video stream, the lifecycle of the audio stream is maintained by the SDL library. When you receive the SDLAudioStreamDidStartNotification, you can begin streaming audio.

Audio Stream Manager

The SDLAudioStreamManager 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.

Playing from File

Objective-C
[self.sdlManager.streamManager.audioManager pushWithFileURL:audioFileURL];
[self.sdlManager.streamManager.audioManager playNextWhenReady];
Swift
sdlManager.streamManager?.audioManager.push(withFileURL: url)
sdlManager.streamManager?.audioManager.playNextWhenReady()

Playing from Data

Objective-C
[self.sdlManager.streamManager.audioManager pushWithData:audioData];
[self.sdlManager.streamManager.audioManager playNextWhenReady];
Swift
sdlManager.streamManager?.audioManager.push(withData: audioData)
sdlManager.streamManager?.audioManager.playNextWhenReady()

Implementing the Delegate

Objective-C
- (void)audioStreamManager:(SDLAudioStreamManager *)audioManager errorDidOccurForFile:(NSURL *)fileURL error:(NSError *)error {

}

- (void)audioStreamManager:(SDLAudioStreamManager *)audioManager errorDidOccurForDataBuffer:(NSError *)error {

}

- (void)audioStreamManager:(SDLAudioStreamManager *)audioManager fileDidFinishPlaying:(NSURL *)fileURL successfully:(BOOL)successfully {
    if (audioManager.queue.count != 0) {
        [audioManager playNextWhenReady];
    }
}

- (void)audioStreamManager:(SDLAudioStreamManager *)audioManager dataBufferDidFinishPlayingSuccessfully:(BOOL)successfully {
    if (audioManager.queue.count != 0) {
        [audioManager playNextWhenReady];
    }
}
Swift
func audioStreamManager(_ audioManager: SDLAudioStreamManager, errorDidOccurForFile fileURL: URL, error: Error) {

}

func audioStreamManager(_ audioManager: SDLAudioStreamManager, errorDidOccurForDataBuffer error: Error) {

}

func audioStreamManager(_ audioManager: SDLAudioStreamManager, fileDidFinishPlaying fileURL: URL, successfully: Bool) {
    if audioManager.queue.count != 0 {
        audioManager.playNextWhenReady()
    }
}

func audioStreamManager(_ audioManager: SDLAudioStreamManager, dataBufferDidFinishPlayingSuccessfully successfully: Bool) {
    if audioManager.queue.count != 0 {
        audioManager.playNextWhenReady()
    }
}

Manually Sending Data

Once the audio stream is connected, data may be easily passed to the Head Unit. The function sendAudioData: provides us with whether or not the PCM Audio Data was successfully transferred to the Head Unit. If your app is in a state that it is unable to send audio data, this method will return a failure. If successful playback will begin immediately.

Objective-C
NSData* audioData = <#Acquire Audio Data#>;

if (![self.sdlManager.streamManager sendAudioData:audioData]) {
    <#Could not send audio data#>
}
Swift
let audioData = <#Acquire Audio Data#>

guard let streamManager = self.sdlManager.streamManager, streamManager.isAudioConnected else { return }

if !streamManager.sendAudioData(audioData) {
    <#Could not send audio data#>
}
View on GitHub.com
Previous Section Next Section