Android上多个应用不能同时调用AudioRecord

http://stackoverflow.com/questions/6646313/android-test-if-audio-record-focus-is-available

I am using AudioRecord to record audio for further proccessing. It seems that only one AudioRecord at a time can have access to the audio hardware. How can i check if the audio hardware is accessible, so that i can start recording? One possibility is to instantiate a AudioRecord instance and then check its state with AudioRecord.getState(). This will return, wether the AudioRecord-Instance was initialized properly. Is this the only way to do so? But there is another problem, which you have to address when doing audiorecording. What happens when another application wants to record? Is there any possibility to be notified if another application wants to record?

 

也就是说如果一个应用在使用音频输入,另一个应用就不能同时使用,否则,会返回错误,log如下:

failed: other input already started

 

看下代码就明白了:

/hardware/libhardware_legacy/audio/AudioPolicyManagerBase.cpp

status_t AudioPolicyManagerBase::startInput(audio_io_handle_t input) {
    ...
    // refuse 2 active AudioRecord clients at the same time
    if (getActiveInput() != 0) { 
        LOGW("startInput() input %d failed: other input already started", input);
        return INVALID_OPERATION;
    }
    ...
}

 

另外,AudioTrack有时候打印这样的log:

 
Underrun user: fb0, server: fb0, flags 0003

 下面是underrun和overrun的解释:

  • underrun

In computing, buffer underrun or buffer underflow is a state occurring when a buffer used to communicate between two devices or processes is fed with data at a lower speed than the data is being read from it. This requires the program or device reading from the buffer to pause its processing while the buffer refills. This can cause undesired and sometimes serious side effects because the data being buffered is generally not suited to stop-start access of this kind.

  • overrun

In computer security and programming, a buffer overflow, or buffer overrun, is an anomaly where a program, while writing data to a buffer, overruns the buffer's boundary and overwrites adjacent memory. This is a special case of violation of memory safety.

Buffer overflows can be triggered by inputs that are designed to execute code, or alter the way the program operates. This may result in erratic program behavior, including memory access errors, incorrect results, a crash, or a breach of system security. Thus, they are the basis of many software vulnerabilities and can be maliciously exploited.

Programming languages commonly associated with buffer overflows include C and C++, which provide no built-in protection against accessing or overwriting data in any part of memory and do not automatically check that data written to an array (the built-in buffer type) is within the boundaries of that array. Bounds checking can prevent buffer overflows.

你可能感兴趣的:(Android)