use of android.media.AudioRecord in project robolectric by robolectric.
the class ShadowAudioRecordTest method nativeReadShortCallsAudioRecordSourceWhenSetBlockingMOnwards.
@Test
@Config(minSdk = M)
public void nativeReadShortCallsAudioRecordSourceWhenSetBlockingMOnwards() {
AudioRecordSource source = Mockito.mock(AudioRecordSource.class);
ShadowAudioRecord.setSource(source);
AudioRecord audioRecord = createAudioRecord();
audioRecord.startRecording();
audioRecord.read(new short[100], 0, 100, AudioRecord.READ_BLOCKING);
verify(source).readInShortArray(any(short[].class), eq(0), eq(100), /* isBlocking=*/
eq(true));
verifyNoMoreInteractions(source);
}
use of android.media.AudioRecord in project Conversations by siacs.
the class AppRTCAudioManager method isMicrophoneAvailable.
public static boolean isMicrophoneAvailable() {
microphoneLatch = new CountDownLatch(1);
AudioRecord audioRecord = null;
boolean available = true;
try {
final int sampleRate = 44100;
final int channel = AudioFormat.CHANNEL_IN_MONO;
final int format = AudioFormat.ENCODING_PCM_16BIT;
final int bufferSize = AudioRecord.getMinBufferSize(sampleRate, channel, format);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channel, format, bufferSize);
audioRecord.startRecording();
final short[] buffer = new short[bufferSize];
final int audioStatus = audioRecord.read(buffer, 0, bufferSize);
if (audioStatus == AudioRecord.ERROR_INVALID_OPERATION || audioStatus == AudioRecord.STATE_UNINITIALIZED)
available = false;
} catch (Exception e) {
available = false;
} finally {
release(audioRecord);
}
microphoneLatch.countDown();
return available;
}
use of android.media.AudioRecord in project SinVoice by JesseGu.
the class Record method start.
public void start() {
if (STATE_STOP == mState) {
switch(mChannel) {
case CHANNEL_1:
mChannelConfig = AudioFormat.CHANNEL_IN_MONO;
break;
case CHANNEL_2:
mChannelConfig = AudioFormat.CHANNEL_IN_STEREO;
break;
}
switch(mBits) {
case BITS_8:
mAudioEncoding = AudioFormat.ENCODING_PCM_8BIT;
break;
case BITS_16:
mAudioEncoding = AudioFormat.ENCODING_PCM_16BIT;
break;
}
int minBufferSize = AudioRecord.getMinBufferSize(mFrequence, mChannelConfig, mAudioEncoding);
LogHelper.d(TAG, "minBufferSize:" + minBufferSize);
if (mBufferSize >= minBufferSize) {
AudioRecord record = new AudioRecord(MediaRecorder.AudioSource.MIC, mFrequence, mChannelConfig, mAudioEncoding, mBufferSize);
if (null != record) {
try {
mState = STATE_START;
record.startRecording();
LogHelper.d(TAG, "record start");
if (null != mCallback) {
if (null != mListener) {
mListener.onStartRecord();
}
while (STATE_START == mState) {
BufferData data = mCallback.getRecordBuffer();
if (null != data) {
if (null != data.mData) {
int bufferReadResult = record.read(data.mData, 0, mBufferSize);
data.setFilledSize(bufferReadResult);
mCallback.freeRecordBuffer(data);
} else {
// end of input
LogHelper.d(TAG, "get end input data, so stop");
break;
}
} else {
LogHelper.e(TAG, "get null data");
break;
}
}
if (null != mListener) {
mListener.onStopRecord();
}
}
record.stop();
record.release();
LogHelper.d(TAG, "record stop");
} catch (IllegalStateException e) {
e.printStackTrace();
LogHelper.e(TAG, "start record error");
}
mState = STATE_STOP;
}
} else {
LogHelper.e(TAG, "bufferSize is too small");
}
}
}
use of android.media.AudioRecord in project AndroidSDK-RecipeBook by gabu.
the class SoundSwitch method run.
// スレッド開始(録音を開始)
public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
int bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
short[] buffer = new short[bufferSize];
audioRecord.startRecording();
while (isRecoding) {
audioRecord.read(buffer, 0, bufferSize);
short max = 0;
for (int i = 0; i < bufferSize; i++) {
// 最大音量を計算
max = (short) Math.max(max, buffer[i]);
// 最大音量がボーダーを超えていたら
if (max > mBorderVolume) {
if (mListener != null) {
// リスナーを実行
mListener.onReachedVolume(max);
break;
}
}
}
}
audioRecord.stop();
audioRecord.release();
}
Aggregations