Java Code Examples for android.media.MediaRecorder#reset()

The following examples show how to use android.media.MediaRecorder#reset() . 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: RecordService.java    From android-auto-call-recorder with MIT License 6 votes vote down vote up
private void startAndSaveRecord(File recordFile) {
    stopRecord();
    mMediaRecorder = new MediaRecorder();
    mMediaRecorder.reset();
    // TODO: 16.05.17 not working well
    mMediaRecorder.setAudioSource(mPreferenceHelper.getAudioSource());   //or default voice_communication
    mMediaRecorder.setOutputFormat(mPreferenceHelper.getOutputFormat()); //MP4
    mMediaRecorder.setAudioEncoder(mPreferenceHelper.getAudioEncoder()); //AAC
    mMediaRecorder.getMaxAmplitude();
    try {
        if (!recordFile.createNewFile()) {
            Log.i(TAG, "File name has been already given");
        }
        mMediaRecorder.setOutputFile(recordFile.getAbsolutePath());
        mMediaRecorder.prepare();
        mMediaRecorder.start();
    } catch (IOException e) {
        Log.e(TAG, "Error: Recording could not be starting!", e);
    }
}
 
Example 2
Source File: MediaRecorderSystemImpl.java    From FamilyChat with Apache License 2.0 6 votes vote down vote up
@Override
public void initRecorder(Camera camera, int cameraId, Surface surface, String filePath)
{
    mMediaRecorder = new MediaRecorder();
    mMediaRecorder.reset();
    if (camera != null)
        mMediaRecorder.setCamera(camera);
    mMediaRecorder.setOnErrorListener(this);
    mMediaRecorder.setPreviewDisplay(surface);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//视频源
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//音频源
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);//视频输出格式 也可设为3gp等其他格式
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//音频格式
    mMediaRecorder.setVideoSize(640, 480);//设置分辨率,市面上大多数都支持640*480
    //        mediaRecorder.setVideoFrameRate(25);//设置每秒帧数 这个设置有可能会出问题,有的手机不支持这种帧率就会录制失败,这里使用默认的帧率,当然视频的大小肯定会受影响
    //这里设置可以调整清晰度
    mMediaRecorder.setVideoEncodingBitRate(2 * 1024 * 512);

    if (cameraId == Camera.CameraInfo.CAMERA_FACING_BACK)
        mMediaRecorder.setOrientationHint(90);
    else
        mMediaRecorder.setOrientationHint(270);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);//视频录制格式
    mMediaRecorder.setOutputFile(filePath);
}
 
Example 3
Source File: AudioRecorder.java    From PrivacyStreams with Apache License 2.0 5 votes vote down vote up
static Audio recordAudio(UQI uqi, long duration) throws IOException {
    List<Integer> amplitudes = new ArrayList<>();

    MediaRecorder recorder = new MediaRecorder();
    recorder.setAudioSource(Globals.AudioConfig.audioSource);
    recorder.setOutputFormat(Globals.AudioConfig.outputFormat);
    recorder.setAudioEncoder(Globals.AudioConfig.audioEncoder);

    String audioPath = "temp/audio_" + TimeUtils.getTimeTag() + ".amr";
    File tempAudioFile = StorageUtils.getValidFile(uqi.getContext(), audioPath, false);
    recorder.setOutputFile(tempAudioFile.getAbsolutePath());

    recorder.prepare();
    recorder.start();   // Recording is now started

    long startTime = System.currentTimeMillis();
    while (true) {
        long currentTime = System.currentTimeMillis();
        if (currentTime - startTime > duration) {
            break;
        }
        amplitudes.add(recorder.getMaxAmplitude());
    }

    recorder.stop();
    recorder.reset();   // You can reuse the object by going back to setAudioSource() step
    recorder.release(); // Now the object cannot be reused

    AudioData audioData = AudioData.newTempRecord(tempAudioFile, amplitudes);

    return new Audio(startTime, audioData);
}
 
Example 4
Source File: VideoRecordSurface.java    From VideoRecord with Apache License 2.0 5 votes vote down vote up
@Override
public void onError(MediaRecorder mr, int what, int extra) {
    try {
        if (mr != null)
            mr.reset();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 5
Source File: JCameraView.java    From CameraView with Apache License 2.0 5 votes vote down vote up
private void startRecord() {
    mediaRecorder = new MediaRecorder();
    mediaRecorder.reset();
    mCamera.unlock();
    // 设置录制视频源为Camera(相机)
    mediaRecorder.setCamera(mCamera);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    // 设置录制完成后视频的封装格式THREE_GPP为3gp.MPEG_4为mp4
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    // 设置录制的视频编码h263 h264
    mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    // 设置视频录制的分辨率。必须放在设置编码和格式的后面,否则报错
    mediaRecorder.setVideoSize(width, height);
    // 设置录制的视频帧率。必须放在设置编码和格式的后面,否则报错
    if (SELECTED_CAMERA == CAMERA_FRONT_POSITION) {
        mediaRecorder.setOrientationHint(270);
    } else {
        mediaRecorder.setOrientationHint(90);
    }
    mediaRecorder.setMaxDuration(10000);
    mediaRecorder.setVideoEncodingBitRate(5 * 1024 * 1024);
    mediaRecorder.setVideoFrameRate(20);
    mediaRecorder.setPreviewDisplay(mHolder.getSurface());
    // 设置视频文件输出的路径
    mediaRecorder.setOutputFile("/sdcard/love.mp4");
    try {
        //准备录制
        mediaRecorder.prepare();
        // 开始录制
        mediaRecorder.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: AudioRecorder.java    From RxAndroidAudio with MIT License 5 votes vote down vote up
/**
 * prepare for a new audio record.
 */
@WorkerThread
public synchronized boolean prepareRecord(int audioSource, int outputFormat, int audioEncoder,
        int sampleRate, int bitRate, File outputFile) {
    stopRecord();

    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(audioSource);
    mRecorder.setOutputFormat(outputFormat);
    mRecorder.setAudioSamplingRate(sampleRate);
    mRecorder.setAudioEncodingBitRate(bitRate);
    mRecorder.setAudioEncoder(audioEncoder);
    mRecorder.setOutputFile(outputFile.getAbsolutePath());

    // Handle IOException
    try {
        mRecorder.prepare();
    } catch (IOException exception) {
        Log.w(TAG, "startRecord fail, prepare fail: " + exception.getMessage());
        setError(ERROR_INTERNAL);
        mRecorder.reset();
        mRecorder.release();
        mRecorder = null;
        return false;
    }
    mState = STATE_PREPARED;
    return true;
}
 
Example 7
Source File: MediaRecorderSystemImpl.java    From FamilyChat with Apache License 2.0 5 votes vote down vote up
@Override
public void onError(MediaRecorder mr, int what, int extra)
{
    try
    {
        if (mr != null)
            mr.reset();
    } catch (Exception e)
    {
        mIsRecording = false;
    }
}