简体   繁体   English

检查麦克风是否已在Linux上与C一起使用

[英]Check if microphone is already being used with C on Linux

I was playing around with the Pulseaudio API and due to my little knowledge on how the sound system works, I'm not really understanding why it's possible having multiple applications using the mic at the same time. 我一直在玩Pulseaudio API,由于我对声音系统的工作原理了解甚少,所以我并不太了解为什么可能同时使用多个应用程序使用麦克风。

Or to better phrase it: why the fprintf is not called I have 2 applications that are actively recording stuffs and I start the following program? 或更准确地说:为什么不调用fprintf我有2个正在主动记录内容的应用程序,然后启动以下程序?

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#define BUFSIZE 1024


int main(int argc, char*argv[]) {
    /* The sample type to use */
    static const pa_sample_spec ss = {
        .format = PA_SAMPLE_S16LE,
        .rate = 44100,
        .channels = 2
    };
    pa_simple *s = NULL;
    int ret = 1;
    int error;

    /* Create the recording stream */
    if (!(s = 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));

    return 0;
}

I'd guess you're not getting an error message because the program is successfully creating a pa_simple connection to the pulse audio server. 我猜您没有收到错误消息,因为该程序已成功创建与脉冲音频服务器的pa_simple连接。

You may wish to add pa_simple_free(s); 您可能希望添加pa_simple_free(s); to the end of your main() before you return. 返回之前,将其移至main()的末尾。

Also, here is a link to an example pa_simple record program: parec-simple_8c-example 另外,这里是指向示例pa_simple记录程序的链接: parec-simple_8c-example

See Pulse Audio API at freedesktop.org 请参阅freedesktop.org上的Pulse Audio API


EDIT: Your question "why the fprintf is not called" is because the Pulse Audio Simple API doesn't guarantee exclusive access to the Microphone. 编辑:您的问题“为什么不调用fprintf”是因为Pulse Audio Simple API不保证对麦克风的专有访问。 Therefor, when two other applications are already "using" the microphone, and you create a simple stream connection to the server, there is no error generated. 因此,当另外两个应用程序已经在“使用”麦克风,并且您创建了到服务器的简单流连接时,不会产生任何错误。 Is your question really "How to determine if the microphone is being used by any program - and which one"? 您的问题是否真的是“如何确定某个程序是否正在使用麦克风-以及哪个程序正在使用”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM