Search in sources :

Example 61 with AudioRecord

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);
}
Also used : AudioRecord(android.media.AudioRecord) AudioRecordSource(org.robolectric.shadows.ShadowAudioRecord.AudioRecordSource) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 62 with AudioRecord

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;
}
Also used : AudioRecord(android.media.AudioRecord) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 63 with AudioRecord

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");
        }
    }
}
Also used : AudioRecord(android.media.AudioRecord) BufferData(com.libra.sinvoice.Buffer.BufferData)

Example 64 with AudioRecord

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();
}
Also used : AudioRecord(android.media.AudioRecord)

Aggregations

AudioRecord (android.media.AudioRecord)64 Test (org.junit.Test)22 AudioRecordSource (org.robolectric.shadows.ShadowAudioRecord.AudioRecordSource)13 AssetFileDescriptor (android.content.res.AssetFileDescriptor)12 AudioEffect (android.media.audiofx.AudioEffect)12 LargeTest (android.test.suitebuilder.annotation.LargeTest)12 Config (org.robolectric.annotation.Config)12 AudioFormat (android.media.AudioFormat)6 SystemApi (android.annotation.SystemApi)5 ByteBuffer (java.nio.ByteBuffer)4 SuppressLint (android.annotation.SuppressLint)1 MediaFormat (android.media.MediaFormat)1 MediaRecorder (android.media.MediaRecorder)1 Handler (android.os.Handler)1 RequiresPermission (androidx.annotation.RequiresPermission)1 AudioDispatcher (be.tarsos.dsp.AudioDispatcher)1 TarsosDSPAudioFormat (be.tarsos.dsp.io.TarsosDSPAudioFormat)1 TarsosDSPAudioInputStream (be.tarsos.dsp.io.TarsosDSPAudioInputStream)1 BufferData (com.libra.sinvoice.Buffer.BufferData)1 ActorCreator (im.actor.runtime.actors.ActorCreator)1