【问题标题】:PulseAudio recording and playback failsPulseAudio 录制和播放失败
【发布时间】:2013-01-05 11:09:13
【问题描述】:

我正在尝试使用 centOS 6.2 中的脉冲音频 API 录制和播放音频数据。但它什么也不记录和回放。我正在使用来自pulseaudio 的代码。我需要帮助才能让它在我的电脑上运行。我该怎么办?我的代码也在下面给出-

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

#include <pulse/simple.h>
#include <pulse/error.h>

#define BUFSIZE 32

int main(int argc, char*argv[]) {

/* The Sample format to use */
static const pa_sample_spec ss = {
    .format = PA_SAMPLE_S16LE,
    .rate = 44100,
    .channels = 2
};

pa_simple *s_in, *s_out = NULL;
int ret = 1;
int error;


/* Create a new playback stream */
if (!(s_out = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) {
    fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
    goto finish;
}

  if (!(s_in = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) {
    fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
    goto finish;
}

for (;;) {
    uint8_t buf[BUFSIZE];
    ssize_t r;

#if 1
    pa_usec_t latency;

    if ((latency = pa_simple_get_latency(s_in, &error)) == (pa_usec_t) -1) {
        fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error));
        goto finish;
    }

    fprintf(stderr, "In:  %0.0f usec    \r\n", (float)latency);

    if ((latency = pa_simple_get_latency(s_out, &error)) == (pa_usec_t) -1) {
        fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error));
        goto finish;
    }

    fprintf(stderr, "Out: %0.0f usec    \r\n", (float)latency);
#endif

    if (pa_simple_read(s_in, buf, sizeof(buf), &error) < 0) {

        fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
        goto finish;
    }

    /* ... and play it */
    if (pa_simple_write(s_out, buf, sizeof(buf), &error) < 0) {
        fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
        goto finish;
    }
}

/* Make sure that every single sample was played */
if (pa_simple_drain(s_out, &error) < 0) {
    fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));
    goto finish;
}

ret = 0;

finish:

if (s_in)
    pa_simple_free(s_in);
if (s_out)
    pa_simple_free(s_out);

return ret;
}

【问题讨论】:

  • 到目前为止你做了什么来调试它,它走了多远,你发现了什么?
  • 它只是打印延迟但不播放任何声音。
  • 请不要使用gotos。
  • @David 代码取自 pulseaudio 的示例文件。

标签: c linux centos pulseaudio


【解决方案1】:

成功完成。这是我的操作系统中的一个错误,因为我有一段时间没有更新插件了。重新安装操作系统后,它工作。 谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-19
    • 2016-01-21
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 2021-04-06
    • 1970-01-01
    相关资源
    最近更新 更多