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 SDLStreamingMediaManager
class. A reference to this class is available from the SDLManager
's streamManager
property.
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.
Like the lifecycle of the video stream, the lifecycle of the audio stream is maintained by the SDL library, therefore, you do not need to start the audio stream if you've set a streaming configuration when starting your SDLManager. When you receive the SDLAudioStreamDidStartNotification, you can begin streaming audio.
[self.sdlManager.streamManager.audioManager pushWithFileURL:audioFileURL]; [self.sdlManager.streamManager.audioManager playNextWhenReady];
sdlManager.streamManager?.audioManager.push(withFileURL: url) sdlManager.streamManager?.audioManager.playNextWhenReady()
[self.sdlManager.streamManager.audioManager pushWithData:audioData]; [self.sdlManager.streamManager.audioManager playNextWhenReady];
sdlManager.streamManager?.audioManager.push(with: audioData) sdlManager.streamManager?.audioManager.playNextWhenReady()
- (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]; } }
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() } }
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.
NSData *audioData = <#Acquire Audio Data#>; if (![self.sdlManager.streamManager sendAudioData:audioData]) { <#Could not send audio data#> }
let audioData = <#Acquire Audio Data#> guard let streamManager = self.sdlManager.streamManager, streamManager.isAudioConnected else { return } if !streamManager.sendAudioData(audioData) { <#Could not send audio data#> }