Java Code Examples for android.media.AudioRecord#getRecordingState()

The following examples show how to use android.media.AudioRecord#getRecordingState() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AndroidRecorder.java    From LingoRecorder with Apache License 2.0 6 votes vote down vote up
@Override
public void startRecording() throws Exception {
    int buffSize = getBufferSize();

    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, recorderProperty.getSampleRate(),
            channels, audioFormat, buffSize);

    if (recorder.getState() != AudioRecord.STATE_INITIALIZED) {
        throw new RecorderInitException();
    }

    payloadSize = 0;
    recorder.startRecording();

    if (recorder.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
        throw new RecorderStartException();
    }
}
 
Example 2
Source File: MicRecorder.java    From ScreenCapture with MIT License 5 votes vote down vote up
/**
 * NOTE: Should waiting all output buffer disappear queue input buffer
 */
private void feedAudioEncoder(int index) {
    if (index < 0 || mForceStop.get()) return;
    final AudioRecord r = Objects.requireNonNull(mMic, "maybe release");
    final boolean eos = r.getRecordingState() == AudioRecord.RECORDSTATE_STOPPED;
    final ByteBuffer frame = mEncoder.getInputBuffer(index);
    int offset = frame.position();
    int limit = frame.limit();
    int read = 0;
    if (!eos) {
        read = r.read(frame, limit);
        if (VERBOSE) Log.d(TAG, "Read frame data size " + read + " for index "
                + index + " buffer : " + offset + ", " + limit);
        if (read < 0) {
            read = 0;
        }
    }

    long pstTs = calculateFrameTimestamp(read << 3);
    int flags = BUFFER_FLAG_KEY_FRAME;

    if (eos) {
        flags = BUFFER_FLAG_END_OF_STREAM;
    }
    // feed frame to encoder
    if (VERBOSE) Log.d(TAG, "Feed codec index=" + index + ", presentationTimeUs="
            + pstTs + ", flags=" + flags);
    mEncoder.queueInputBuffer(index, offset, read, pstTs, flags);
}
 
Example 3
Source File: CheckPermission.java    From EasyPhotos with Apache License 2.0 4 votes vote down vote up
/**
 * 用于检测是否具有录音权限
 *
 * @return
 */
public static int getRecordState() {
    int minBuffer = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat
            .ENCODING_PCM_16BIT);
    AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, 44100, AudioFormat
            .CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, (minBuffer * 100));
    short[] point = new short[minBuffer];
    int readSize = 0;
    try {

        audioRecord.startRecording();//检测是否可以进入初始化状态
    } catch (Exception e) {
        if (audioRecord != null) {
            audioRecord.release();
            audioRecord = null;
        }
        return STATE_NO_PERMISSION;
    }
    if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
        //6.0以下机型都会返回此状态,故使用时需要判断bulid版本
        //检测是否在录音中
        if (audioRecord != null) {
            audioRecord.stop();
            audioRecord.release();
            audioRecord = null;
            LogUtil.d("录音机被占用");
        }
        return STATE_RECORDING;
    } else {
        //检测是否可以获取录音结果

        readSize = audioRecord.read(point, 0, point.length);


        if (readSize <= 0) {
            if (audioRecord != null) {
                audioRecord.stop();
                audioRecord.release();
                audioRecord = null;

            }
            LogUtil.d("录音的结果为空");
            return STATE_NO_PERMISSION;

        } else {
            if (audioRecord != null) {
                audioRecord.stop();
                audioRecord.release();
                audioRecord = null;

            }

            return STATE_SUCCESS;
        }
    }
}
 
Example 4
Source File: CheckPermission.java    From imsdk-android with MIT License 4 votes vote down vote up
/**
 * 用于检测是否具有录音权限
 *
 * @return
 */
public static int getRecordState() {
    int minBuffer = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat
            .ENCODING_PCM_16BIT);
    AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, 44100, AudioFormat
            .CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, (minBuffer * 100));
    short[] point = new short[minBuffer];
    int readSize = 0;
    try {

        audioRecord.startRecording();//检测是否可以进入初始化状态
    } catch (Exception e) {
        if (audioRecord != null) {
            audioRecord.release();
            audioRecord = null;
        }
        return STATE_NO_PERMISSION;
    }
    if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
        //6.0以下机型都会返回此状态,故使用时需要判断bulid版本
        //检测是否在录音中
        if (audioRecord != null) {
            audioRecord.stop();
            audioRecord.release();
            audioRecord = null;
            Log.d("CheckAudioPermission", "录音机被占用");
        }
        return STATE_RECORDING;
    } else {
        //检测是否可以获取录音结果

        readSize = audioRecord.read(point, 0, point.length);


        if (readSize <= 0) {
            if (audioRecord != null) {
                audioRecord.stop();
                audioRecord.release();
                audioRecord = null;

            }
            Log.d("CheckAudioPermission", "录音的结果为空");
            return STATE_NO_PERMISSION;

        } else {
            if (audioRecord != null) {
                audioRecord.stop();
                audioRecord.release();
                audioRecord = null;

            }

            return STATE_SUCCESS;
        }
    }
}
 
Example 5
Source File: CheckPermission.java    From imsdk-android with MIT License 4 votes vote down vote up
/**
 * 用于检测是否具有录音权限
 *
 * @return
 */
public static int getRecordState() {
    int minBuffer = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat
            .ENCODING_PCM_16BIT);
    AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, 44100, AudioFormat
            .CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, (minBuffer * 100));
    short[] point = new short[minBuffer];
    int readSize = 0;
    try {

        audioRecord.startRecording();//检测是否可以进入初始化状态
    } catch (Exception e) {
        if (audioRecord != null) {
            audioRecord.release();
            audioRecord = null;
        }
        return STATE_NO_PERMISSION;
    }
    if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
        //6.0以下机型都会返回此状态,故使用时需要判断bulid版本
        //检测是否在录音中
        if (audioRecord != null) {
            audioRecord.stop();
            audioRecord.release();
            audioRecord = null;
            Log.d("CheckAudioPermission", "录音机被占用");
        }
        return STATE_RECORDING;
    } else {
        //检测是否可以获取录音结果

        readSize = audioRecord.read(point, 0, point.length);


        if (readSize <= 0) {
            if (audioRecord != null) {
                audioRecord.stop();
                audioRecord.release();
                audioRecord = null;

            }
            Log.d("CheckAudioPermission", "录音的结果为空");
            return STATE_NO_PERMISSION;

        } else {
            if (audioRecord != null) {
                audioRecord.stop();
                audioRecord.release();
                audioRecord = null;

            }

            return STATE_SUCCESS;
        }
    }
}
 
Example 6
Source File: CheckPermissionUtil.java    From TikTok with Apache License 2.0 4 votes vote down vote up
/**
     * 判断是是否有录音权限
     */
    public static boolean isHasAudioPermission(final Context context){
        int bufferSizeInBytes = 0;
        bufferSizeInBytes = AudioRecord.getMinBufferSize(44100,
                AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
        AudioRecord audioRecord =  new AudioRecord(MediaRecorder.AudioSource.MIC, 44100,
                AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes);
        //开始录制音频
        //开始录制音频
        try{
            // 防止某些手机崩溃,例如联想
            audioRecord.startRecording();
        }catch (IllegalStateException e){
            e.printStackTrace();
//            AVLogUtils.e(TAG, Log.getStackTraceString(e));
        }
        /**
         * 根据开始录音判断是否有录音权限
         */
        if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING
                && audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED) {
//            AVLogUtils.e(TAG, "audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING : " + audioRecord.getRecordingState());
            return false;
        }

        if (audioRecord.getRecordingState() == AudioRecord.RECORDSTATE_STOPPED) {
            //如果短时间内频繁检测,会造成audioRecord还未销毁完成,此时检测会返回RECORDSTATE_STOPPED状态,再去read,会读到0的size,可以更具自己的需求返回true或者false
            return false;
        }

        byte[] bytes = new byte[1024];
        int readSize = audioRecord.read(bytes, 0, 1024);
        if (readSize == AudioRecord.ERROR_INVALID_OPERATION || readSize <= 0) {
//            AVLogUtils.e(TAG, "readSize illegal : " + readSize);
            return false;
        }
        audioRecord.stop();
        audioRecord.release();
        audioRecord = null;

        return true;
    }
 
Example 7
Source File: AudioRecorder.java    From TikTok with Apache License 2.0 4 votes vote down vote up
@Override
    public void run() {
        try {
            //初始化音频
            int bufferSizeInBytes = AudioRecord
                    .getMinBufferSize(audioSampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
            final AudioRecord
                    audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, audioSampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes);
            if(audioRecord == null){
                mOnAudioRecorderListener.onNotPermission();
                return ;
            }
            audioRecord.startRecording();

            /**
             * 根据开始录音判断是否有录音权限
             */
            if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING
                    && audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED) {
//            AVLogUtils.e(TAG, "audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING : " + audioRecord.getRecordingState());
                isAudioPermission = false;
            }

            if (audioRecord.getRecordingState() == AudioRecord.RECORDSTATE_STOPPED) {
                //如果短时间内频繁检测,会造成audioRecord还未销毁完成,此时检测会返回RECORDSTATE_STOPPED状态,再去read,会读到0的size,可以更具自己的需求返回true或者false
                isAudioPermission = false;
            }

            if(!isAudioPermission){
                mOnAudioRecorderListener.onNotPermission();
                return ;
            }
            mOnAudioRecorderListener.onCanRecord(isAudioPermission);

            byte[] data = new byte[2048];
            while(isRecord){
                if(audioRecord == null){
                    return ;
                }
                int offset = 0;
                while(offset < 2048) {
                    int readSize = audioRecord.read(data, offset, data.length-offset);
                    offset+=readSize;
                }
                if(isAudioRecordWrite){//写入文件
                    HeyhouRecorder.getInstance().recordAudioNHW(data,audioSampleRate,HeyhouRecorder.FORMAT_S16,1024);
                }

            }
            audioRecord.stop();
            audioRecord.release();
        }catch (Exception e) {
            e.printStackTrace();
            mOnAudioRecorderListener.onRecordError("录音失败");
        }
    }
 
Example 8
Source File: PermissionUtil.java    From AlbumCameraRecorder with MIT License 4 votes vote down vote up
/**
 * 用于检测是否具有录音权限
 *
 * @return 状态:是否具有
 */
public static int getRecordState() {
    int minBuffer = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat
            .ENCODING_PCM_16BIT);
    AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, 44100, AudioFormat
            .CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, (minBuffer * 100));
    short[] point = new short[minBuffer];
    int readSize;
    try {

        audioRecord.startRecording();//检测是否可以进入初始化状态
    } catch (Exception e) {
        if (audioRecord != null) {
            audioRecord.release();
            audioRecord = null;
        }
        return STATE_NO_PERMISSION;
    }
    if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
        //6.0以下机型都会返回此状态,故使用时需要判断bulid版本
        //检测是否在录音中
        if (audioRecord != null) {
            audioRecord.stop();
            audioRecord.release();
            Log.d("CheckAudioPermission", "录音机被占用");
        }
        return STATE_RECORDING;
    } else {
        //检测是否可以获取录音结果

        readSize = audioRecord.read(point, 0, point.length);


        if (readSize <= 0) {
            if (audioRecord != null) {
                audioRecord.stop();
                audioRecord.release();

            }
            Log.d("CheckAudioPermission", "录音的结果为空");
            return STATE_NO_PERMISSION;

        } else {
            if (audioRecord != null) {
                audioRecord.stop();
                audioRecord.release();

            }

            return STATE_SUCCESS;
        }
    }
}
 
Example 9
Source File: CheckPermission.java    From PictureSelector with Apache License 2.0 4 votes vote down vote up
/**
 * 用于检测是否具有录音权限
 *
 * @return
 */
public static int getRecordState() {
    int minBuffer = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat
            .ENCODING_PCM_16BIT);
    AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, 44100, AudioFormat
            .CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, (minBuffer * 100));
    short[] point = new short[minBuffer];
    int readSize = 0;
    try {

        audioRecord.startRecording();//检测是否可以进入初始化状态
    } catch (Exception e) {
        if (audioRecord != null) {
            audioRecord.release();
            audioRecord = null;
        }
        return STATE_NO_PERMISSION;
    }
    if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
        //6.0以下机型都会返回此状态,故使用时需要判断bulid版本
        //检测是否在录音中
        if (audioRecord != null) {
            audioRecord.stop();
            audioRecord.release();
            audioRecord = null;
        }
        return STATE_RECORDING;
    } else {
        //检测是否可以获取录音结果

        readSize = audioRecord.read(point, 0, point.length);


        if (readSize <= 0) {
            if (audioRecord != null) {
                audioRecord.stop();
                audioRecord.release();
                audioRecord = null;

            }
            return STATE_NO_PERMISSION;

        } else {
            if (audioRecord != null) {
                audioRecord.stop();
                audioRecord.release();
                audioRecord = null;

            }

            return STATE_SUCCESS;
        }
    }
}
 
Example 10
Source File: CheckPermission.java    From CameraView with Apache License 2.0 4 votes vote down vote up
/**
 * 用于检测是否具有录音权限
 *
 * @return
 */
public static int getRecordState() {
    int minBuffer = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat
            .ENCODING_PCM_16BIT);
    AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, 44100, AudioFormat
            .CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, (minBuffer * 100));
    short[] point = new short[minBuffer];
    int readSize = 0;
    try {

        audioRecord.startRecording();//检测是否可以进入初始化状态
    } catch (Exception e) {
        if (audioRecord != null) {
            audioRecord.release();
            audioRecord = null;
        }
        return STATE_NO_PERMISSION;
    }
    if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
        //6.0以下机型都会返回此状态,故使用时需要判断bulid版本
        //检测是否在录音中
        if (audioRecord != null) {
            audioRecord.stop();
            audioRecord.release();
            audioRecord = null;
            Log.d("CheckAudioPermission", "录音机被占用");
        }
        return STATE_RECORDING;
    } else {
        //检测是否可以获取录音结果

        readSize = audioRecord.read(point, 0, point.length);


        if (readSize <= 0) {
            if (audioRecord != null) {
                audioRecord.stop();
                audioRecord.release();
                audioRecord = null;

            }
            Log.d("CheckAudioPermission", "录音的结果为空");
            return STATE_NO_PERMISSION;

        } else {
            if (audioRecord != null) {
                audioRecord.stop();
                audioRecord.release();
                audioRecord = null;

            }

            return STATE_SUCCESS;
        }
    }
}
 
Example 11
Source File: AudioSource.java    From science-journal with Apache License 2.0 4 votes vote down vote up
private void start() {
  // FYI: the current thread holds lockAudioReceivers.
  // Use VOICE_COMMUNICATION to filter out audio coming from the speakers
  final AudioRecord audioRecord =
      new AudioRecord(
          MediaRecorder.AudioSource.VOICE_COMMUNICATION,
          SAMPLE_RATE_IN_HZ,
          CHANNEL_CONFIG,
          AUDIO_FORMAT,
          minBufferSizeInBytes);
  if (audioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
    audioRecord.release();
    return;
  }

  audioRecord.startRecording();
  // AudioRecord.startRecording() logs an error but it has no return value and
  // doesn't throw an exception when someone else is using the mic.
  if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
    audioRecord.release();
    return;
  }

  running.set(true);
  future =
      executorService.submit(
          () -> {
            short[] buffer = new short[minBufferSizeInBytes / 2];
            int offset = 0;
            boolean goodDataRead = false;

            while (running.get()) {
              int readShorts = audioRecord.read(buffer, offset, buffer.length - offset);
              // On some devices (Moto E, for example) we get a bunch of zeros when we first
              // start reading. Ignore those zeros.
              if (!goodDataRead) {
                int countLeadingZeros = 0;
                while (countLeadingZeros < readShorts && buffer[countLeadingZeros] == 0) {
                  countLeadingZeros++;
                }
                if (countLeadingZeros > 0) {
                  if (readShorts > countLeadingZeros) {
                    System.arraycopy(
                        buffer, countLeadingZeros, buffer, 0, readShorts - countLeadingZeros);
                  }
                  readShorts -= countLeadingZeros;
                }
                goodDataRead = (readShorts > 0);
              }
              offset += readShorts;
              // If the buffer is full, call the Receivers.
              if (offset == buffer.length) {
                synchronized (lockAudioReceivers) {
                  for (AudioReceiver audioReceiver : audioReceivers) {
                    audioReceiver.onReceiveAudio(buffer);
                  }
                }
                offset = 0;
              }
            }

            audioRecord.stop();
            audioRecord.release();
          });
}