我的应用程序需要在发生呼叫时可视化语音,因此我注册了Service
来收听呼叫状态。
所以当onAttachedView
出现时,我会调用扩展Thread
的mic记录类,这是run方法:
public void run() {
try {
mRecordInstance.startRecording();
float splValue = 0.0f;
float rmsValue = 0.0f;
while (this.mIsRunning) {
// creating these variables here so that
// the mode change can be handled
int SIZE = BUFFSIZE;
short[] tempBuffer = new short[SIZE];
mRecordInstance.read(tempBuffer, 0, SIZE);
for (int i = 0; i < SIZE - 1; i++) {
rmsValue += tempBuffer[i] * tempBuffer[i];
}
rmsValue = rmsValue / SIZE;
rmsValue = (float) Math.sqrt(rmsValue);
splValue = (float) (20 * Math.log10(rmsValue / P0));
splValue = splValue + mCaliberationValue;
splValue = (float) round(splValue, 2);
if (mMaxValue < splValue) {
mMaxValue = splValue;
}
if (!mShowMaxValue) {
Message msg = mHandle.obtainMessage(MY_MSG, splValue);
mHandle.sendMessage(msg);
} else {
Message msg = mHandle.obtainMessage(MY_MSG, mMaxValue);
mHandle.sendMessage(msg);
Thread.sleep(2000);
msg = mHandle.obtainMessage(MAXOVER_MSG, mMaxValue);
mHandle.sendMessage(msg);
mShowMaxValue = false;
}
writeLog(splValue);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = mHandle.obtainMessage(ERROR_MSG,
e.getLocalizedMessage()+"");
mHandle.sendMessage(msg);
}
if(mRecordInstance != null){
mRecordInstance.stop();
mRecordInstance.release();
mRecordInstance = null;
}
}
此类通过处理程序将消息发送到可视化语音的类。当通过语音分贝级别绘制一个圆圈时,它的效果非常好。
当我在通话中时,我不接收麦克风输入。 有什么想法吗?
答案 0 :(得分:1)
您的意思是说,您想录制VOICE_DOWNLINK,即语音通话期间收到的音频? 如果是这样,您可以将媒体来源指定为:
m_Recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_DOWNLINK);