Updated: October 26, 2022

Create a handle and open a connection to the preferred audio interface


#include <sys/asoundlib.h>

int snd_pcm_open_preferred( snd_pcm_t **handle, 
                            int        *rcard, 
                            int        *rdevice, 
                            int         mode );


A pointer to a location where snd_pcm_open_preferred() can store a handle for the audio interface. You'll need this handle when you call the other snd_pcm_* functions.
If non-NULL, this must be a pointer to a location where snd_pcm_open_preferred() can store the number of the card that it opened.
If non-NULL, this must be a pointer to a location where snd_pcm_open_preferred() can store the number of the audio device that it opened.
One of:
  • SND_PCM_OPEN_PLAYBACK — open the playback channel (direction).
  • SND_PCM_OPEN_CAPTURE — open the capture channel (direction).

You can OR this flag with any of the above:

  • SND_PCM_OPEN_NONBLOCK — force the mode to be nonblocking. This affects any reading from or writing to the device that you do later; you can query the device any time without blocking.

    You can change the blocking setup later by calling snd_pcm_nonblock_mode().


Use the -l asound option with qcc to link against this library.


The snd_pcm_open_preferred() function is an extension to the snd_pcm_open() function that attempts to open the user-selected default (or preferred) device for the system.

Note: If you use this function, your application is more flexible than if you use snd_pcm_open().

In a system where more than one PCM device exists, you can set a preference for one of these devices. This function attempts to open that device and return a PCM handle to it. The function returns the card and device numbers if the rcard and rdevice arguments aren't NULL.

For information on how io-audio sets the preferred device, see “Preferred device selection” in the io-audio chapter of the QNX Neutrino Utilities Reference.


Zero on success, or a negative value on error.


Invalid mode.
Search permission is denied on a component of the path prefix, or the device exists and the permissions specified are denied.
The open() operation was interrupted by a signal.
Too many file descriptors are currently in use by this process.
Too many files are currently open in the system.
The named device doesn't exist.
The audio driver version is incompatible with the client library that the application uses.
No memory available for data structures.


See the example in Opening your PCM device in the Playing and Capturing Audio Data chapter.


QNX Neutrino

Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Read the Caveats


This function is not thread safe if handle (snd_pcm_t) is used across multiple threads.

Successfully opening a PCM channel doesn't guarantee that there are enough audio resources free to handle your application. Audio resources (e.g. subchannels) are allocated when you configure the channel by calling snd_pcm_channel_params() or snd_pcm_plugin_params() .