找到我可以使用ALSA播放PCM的所有设备

Luc*_*lon 5 linux audio alsa

我使用ALSA播放PCM样本.我用这个函数打开PCM流:

int snd_pcm_open(snd_pcm_t** pcmp,
        const char* name,
        snd_pcm_stream_t stream,
        int mode);
Run Code Online (Sandbox Code Playgroud)

我目前正在使用"default"作为名称参数.我希望能够选择其他设备.我无法理解的是如何确定其他可用设备的名称.

我将USB麦克风连接到我的系统,aplay和amixer似乎检测到了新设备.如何确定该设备的名称?是否有任何ALSA功能可以获取具有各自名称的可用设备列表?

O.C*_*.C. 9

我想你可以使用snd_device_name_hint来枚举设备.这是一个例子.请注意,我还没有编译它!

char **hints;
/* Enumerate sound devices */
int err = snd_device_name_hint(-1, "pcm", (void***)&hints);
if (err != 0)
   return;//Error! Just return

char** n = hints;
while (*n != NULL) {

    char *name = snd_device_name_get_hint(*n, "NAME");

    if (name != NULL && 0 != strcmp("null", name)) {
        //Copy name to another buffer and then free it
        free(name);
    }
    n++;
}//End of while

//Free hint buffer too
snd_device_name_free_hint((void**)hints);
Run Code Online (Sandbox Code Playgroud)