c - Operation not Permitted on ALSA get hw parameters 函数

标签 c alsa

我正在尝试播放从服务器接收的 pcm 音频缓冲区。我下载了一个示例 alsa 播放文件,将录制的文件作为输入,它工作正常,但我在我的 SIP 客户端应用程序中添加的相同代码出现不允许操作错误。打开设备并设置设备配置没问题,但尝试获取我配置的参数时出现Operation not permitted error。 谁能告诉我出现此错误的原因?

/* Open the PCM device in playback mode */
    if (pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE,
                                    SND_PCM_STREAM_PLAYBACK, 0) < 0)
            printf("ERROR: Can't open \"%s\" PCM device. %s\n",
                                    PCM_DEVICE, snd_strerror(pcm));

    /* Allocate parameters object and fill it with default values*/
    snd_pcm_hw_params_alloca(&params);

    snd_pcm_hw_params_any(pcm_handle, params);

    /* Set parameters */
    if (pcm = snd_pcm_hw_params_set_access(pcm_handle, params,
                                    SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
            printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));

    if (pcm = snd_pcm_hw_params_set_format(pcm_handle, params,
                                            //SND_PCM_FORMAT_S16_LE) < 0) 
                                            SND_PCM_FORMAT_MU_LAW) < 0)
            printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));

    if (pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels) < 0)
            printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));

    if (pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0) < 0)
            printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));

    /* Write parameters */
    if (pcm = snd_pcm_hw_params(pcm_handle, params) < 0)
            printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));

   /* Resume information */
    printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));

    printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));

    pcm =  snd_pcm_hw_params_get_channels(params, &tmp);
    printf("channels: %i  %d", tmp, pcm);

    if (tmp == 1)
            printf("(mono)\n");
    else if (tmp == 2)
            printf("(stereo)\n");

    snd_pcm_hw_params_get_rate(params, &tmp, 0);
    printf("rate: %d bps\n", tmp);

    printf("seconds: %d\n", seconds);

    /* Allocate buffer to hold single period */
    snd_pcm_hw_params_get_period_size(params, &frames, 0);

    buff_size = frames * channels * 2 /* 2 -> sample size */;
    buff = (char *) malloc(buff_size);

    printf("buffsize: %d\n", buff_size);
    snd_pcm_hw_params_get_period_time(params, &tmp, NULL);

最佳答案

你所有的错误检查都是错误的。

<运算符绑定(bind)强于 = , 所以在这样一行中:

if (err = snd_something(...) < 0)

将函数的返回值与零进行比较,并将比较结果(假或真,0 或 1)分配给变量。

要使其正常工作,您必须在赋值周围添加括号:

if ((err = snd_something(...)) < 0)

但最好不要尝试将所有内容都放在一个表达式中:

err = snd_something(...);
if (err < 0)

关于c - Operation not Permitted on ALSA get hw parameters 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22830540/

相关文章:

mysql - 使用 C libmysqlclient 无法解释的 MySQL 行为

windows - 我无法使用python播放音频

audio - 如何解决 ffmpeg 卡在 'Opening an input file' 的问题

linux - 修复用户的麦克风音量

c++ - ALSA:如何通过耳机和扬声器发送音频

c - C 中的 "initializer element is not constant char"错误

c - 从 C 中的整数函数返回 NULL

c - Linux 设备驱动程序中的段错误

linux - 如何编写符合 ALSA 的虚拟设备驱动程序?

处理 Node.js TLS 服务器和 C TLS 客户端 (openSSL) 连接的正确方法