# Sample audio apps

Source: [https://docs.qualcomm.com/doc/80-41102-1/topic/sample_audio_apps.html](https://docs.qualcomm.com/doc/80-41102-1/topic/sample_audio_apps.html)

TelAF audio can choose the input and output interfaces to tie together using audio connectors. The audio stream related to a particular interface is represented with an 'Audio Stream Reference'.
    For any stream types, the maximum number of devices supported is one. A single stream per multiple devices is not supported.

## IPC binding an interface

In application.adef add taf\_audio API in bindings.

    bindings:
    {  
        clientExe.clientComponent.taf_audio -> tafAudioService.taf_audio
    }Copy to clipboard

In Component.cdef add the following interface:

    requires:
    {
        api:    
        { 
            taf_audio.api
        }
    }Copy to clipboard

## 
    Audio playback

This section describes how to use the TelAF audio API for audio playback session.

To playback the audio, first get the audio reference of the audio stream.

    /**
    * Audio safe references
    */
    static taf_audio_StreamRef_tAudioRef;
    static taf_audio_StreamRef_tOutRef;
    static taf_audio_ConnectorRef_t   AudioOutputConnectorRef;
    static taf_audio_MediaHandlerRef_tMediaHandlerRef = NULL;
    static taf_audio_StreamRef_tFileAudioRef = NULL;
    static char     AudioFilePath[] = "/data/test.wav";
    static intAudioFileFd = -1;
    
    /**
    * Open Audio Stream for file playback.
    * This function is to be used to play audio file, such as wav and amr
    */
    le_result_t OpenAudioFile()
    {
        le_result_t res;
        OutRef = taf_audio_OpenSpeaker();
        LE_ERROR_IF((OutRef==NULL), "taf_audio_OpenSpeaker returns NULL!");
        AudioOutputConnectorRef  = taf_audio_CreateConnector();
        LE_ERROR_IF((AudioOutputConnectorRef ==NULL), "AudioOutputConnectorRef  is NULL!");
        FileAudioRef = taf_audio_OpenPlayer();
        LE_ERROR_IF((FileAudioRef==NULL), "OpenFilePlayback returns NULL!");
        MediaHandlerRef = taf_audio_AddMediaHandler(FileAudioRef, MyMediaEventHandler, NULL); 
        if (OutRef && FileAudioRef && AudioOutputConnectorRef )
        {
            res = taf_audio_Connect(AudioOutputConnectorRef, OutRef);
            LE_ERROR_IF((res!=LE_OK), "Failed to connect Speaker on Output connector (res %s)!",
            LE_RESULT_TXT(res));
            res = taf_audio_Connect(AudioOutputConnectorRef , FileAudioRef);
            LE_ERROR_IF((res!=LE_OK), "Failed to connect FilePlayback on input connector!"); 
            if ((AudioFileFd=open(AudioFilePath, O_RDONLY)) == -1)
            {
                LE_ERROR("Open file %s failure: errno.%d (%s)",
                AudioFilePath, errno, LE_ERRNO_TXT(errno));
                DisconnectAllAudio(reference);
                return LE_FAULT;
            }
            else
            {
                LE_INFO("Open file %s with AudioFileFd.%d",  AudioFilePath, AudioFileFd);
            }
            res = taf_audio_PlayFile(FileAudioRef, AudioFileFd);
            LE_ERROR_IF((res!=LE_OK), "Failed to play the file!");
        }
        return LE_OK;
    }Copy to clipboard

## Media event handler

Adds a media stream event handler for various audio stream events.

    void MyMediaEventHandler
    (
    taf_audio_StreamRef_t    streamRef,
    taf_audio_MediaEvent_t   event,
    void* contextPtr
    )
    {
        switch(event)
        {
            case TAF_AUDIO_MEDIA_ENDED:
                LE_INFO("File event is TAF_AUDIO_MEDIA_ENDED.");
                if(FileAudioRef)
                {
                    if ((AudioFileFd=open(AudioFilePath, O_RDONLY)) == -1)
                    {
                        LE_ERROR("Open file %s failure: errno.%d (%s)",
                        AudioFilePath, errno, LE_ERRNO_TXT(errno));
                        return;
                    }
                    else
                    {
                        LE_INFO("Open file %s with AudioFileFd.%d",  AudioFilePath, AudioFileFd);
                    }
                    if (taf_audio_PlayFile(FileAudioRef, AudioFileFd) != LE_OK)
                    {
                        LE_ERROR("Failed to play the file");
                        return;
                    }
                    else
                    {
                        LE_INFO("file is now playing.");
                    }
                    break;
                }
            case TAF_AUDIO_MEDIA_ERROR:
                LE_INFO("File event is TAF_AUDIO_MEDIA_ERROR.");
                break;
            case TAF_AUDIO_MEDIA_NO_MORE_SAMPLES:
                LE_INFO("File event is TAF_AUDIO_MEDIA_NO_MORE_SAMPLES.");
                break;
            default:
                LE_INFO("File event is %d", event);
                break;
        }
    }Copy to clipboard

## Close audio stream

Closes the audio stream and connector reference.

    void DisconnectAllAudio()
    {
        LE_INFO("DisconnectAllAudio");
        if(AudioOutputConnectorRef)
        {
            if(OutRef)
            {
                LE_INFO("Disconnect %p from connector.%p", OutRef, AudioOutputConnectorRef);
                taf_audio_Disconnect(AudioOutputConnectorRef, OutRef);
            }
            if(AudioRef)
            {
                 LE_INFO("Disconnect %p from connector.%p", AudioRef, AudioOutputConnectorRef);
                 taf_audio_Disconnect(AudioOutputConnectorRef, AudioRef);
            }
        }
        if(AudioOutputConnectorRef)
        {
            taf_audio_DeleteConnector(AudioOutputConnectorRef);
            AudioOutputConnectorRef = NULL;
        }
        if(AudioOutputConnectorRef)
        {
            if(FileAudioRef)
            {
                LE_INFO("Disconnect %p from connector.%p", FileAudioRef, AudioOutputConnectorRef);
                taf_audio_Disconnect(AudioOutputConnectorRef, FileAudioRef);
            }
            if(OutRef)
            { 
                LE_INFO("Disconnect %p from connector.%p", OutRef, AudioOutputConnectorRef);
                taf_audio_Disconnect(AudioOutputConnectorRef, OutRef);
            }
        }
        if(OutRef)
        {
            taf_audio_Close(OutRef);
            OutRef = NULL;
        }
        if(AudioRef)
        {
            taf_audio_Close(AudioRef);
            AudioRef = NULL;
        }
        if(FileAudioRef)
        {
             taf_audio_Close(FileAudioRef);
             FileAudioRef = NULL;
        }
    }Copy to clipboard

Last Published: May 12, 2026

[Previous Topic
Legacy C App using TelAF API](https://docs.qualcomm.com/bundle/publicresource/80-41102-1/topics/legacy_c_app_using_telaf_api.md) [Next Topic
Configurations](https://docs.qualcomm.com/bundle/publicresource/80-41102-1/topics/audio-configurations.md)