Home > Java > javaTutorial > body text

How to initialize MediaRecorder using Java?

WBOY
Release: 2023-04-21 15:07:08
forward
1409 people have browsed it

Initializing MediaRecorder in java

Implementation code:

private boolean initializeVideo() {  
    Log.v(TAG, "initializeVideo");  
     
    if (mSurfaceHolder == null) {  
      Log.v(TAG, "SurfaceHolder is null");  
      return false;  
    }  
 
    mMediaRecorderRecording = true;  
 
    if (mMediaRecorder == null)  
        mMediaRecorder = new MediaRecorder();  
    else 
        mMediaRecorder.reset();  
    if (mCamera != null) {  
        if (Integer.parseInt(Build.VERSION.SDK) >= 8)  
            VideoCameraNew2.reconnect(mCamera);  
        mCamera.release();  
        mCamera = null;  
    }  
 
    if (useFront && Integer.parseInt(Build.VERSION.SDK) >= 5) {  
            if (isAvailableSprintFFC)  
            {  
                try 
                {  
                    Method method = Class.forName("android.hardware.HtcFrontFacingCamera").getDeclaredMethod("getCamera", null);  
                    mCamera = (Camera) method.invoke(null, null);  
                }  
                catch (Exception ex)  
                {  
                    Log.d(TAG, ex.toString());  
                }  
            } else {  
                mCamera = Camera.open();  
                Camera.Parameters parameters = mCamera.getParameters();  
                parameters.set("camera-id", 2);  
                mCamera.setParameters(parameters);  
            }  
            VideoCameraNew.unlock(mCamera);  
            mMediaRecorder.setCamera(mCamera);  
        mVideoPreview.setOnClickListener(this);  
    }  
    mVideoPreview.setOnLongClickListener(this);  
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);  
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);  
    mMediaRecorder.setOutputFile(sender.getFileDescriptor());  
 
        mMediaRecorder.setVideoFrameRate(20);  
    if (videoQualityHigh) {  
      mMediaRecorder.setVideoSize(352,288);  
    } else {  
      mMediaRecorder.setVideoSize(176,144);  
    }  
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);  
    mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());  
 
    try {  
      mMediaRecorder.prepare();  
      mMediaRecorder.setOnErrorListener(this);  
      mMediaRecorder.start();  
    } catch (IOException exception) {  
      releaseMediaRecorder();  
      finish();  
      return false;  
    }  
    return true;  
  }
Copy after login
private boolean initializeVideo() { 
    Log.v(TAG, "initializeVideo"); 
     
    if (mSurfaceHolder == null) { 
      Log.v(TAG, "SurfaceHolder is null"); 
      return false; 
    } 
 
    mMediaRecorderRecording = true; 
 
    if (mMediaRecorder == null) 
        mMediaRecorder = new MediaRecorder(); 
    else 
        mMediaRecorder.reset(); 
    if (mCamera != null) { 
        if (Integer.parseInt(Build.VERSION.SDK) >= 8) 
            VideoCameraNew2.reconnect(mCamera); 
        mCamera.release(); 
        mCamera = null; 
    } 
 
    if (useFront && Integer.parseInt(Build.VERSION.SDK) >= 5) { 
            if (isAvailableSprintFFC) 
            { 
                try 
                { 
                    Method method = Class.forName("android.hardware.HtcFrontFacingCamera").getDeclaredMethod("getCamera", null); 
                    mCamera = (Camera) method.invoke(null, null); 
                } 
                catch (Exception ex) 
                { 
                    Log.d(TAG, ex.toString()); 
                } 
            } else { 
                mCamera = Camera.open();  
                Camera.Parameters parameters = mCamera.getParameters();  
                parameters.set("camera-id", 2);  
                mCamera.setParameters(parameters);  
            } 
            VideoCameraNew.unlock(mCamera); 
            mMediaRecorder.setCamera(mCamera); 
        mVideoPreview.setOnClickListener(this); 
    } 
    mVideoPreview.setOnLongClickListener(this); 
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    mMediaRecorder.setOutputFile(sender.getFileDescriptor()); 
 
        mMediaRecorder.setVideoFrameRate(20); 
    if (videoQualityHigh) { 
      mMediaRecorder.setVideoSize(352,288); 
    } else { 
      mMediaRecorder.setVideoSize(176,144); 
    } 
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); 
    mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); 
 
    try { 
      mMediaRecorder.prepare(); 
      mMediaRecorder.setOnErrorListener(this); 
      mMediaRecorder.start(); 
    } catch (IOException exception) { 
      releaseMediaRecorder(); 
      finish(); 
      return false; 
    } 
    return true; 
  }
Copy after login
private void releaseMediaRecorder() {  
    Log.v(TAG, "Releasing media recorder.");  
    if (mMediaRecorder != null) {  
      mMediaRecorder.reset();  
      if (mCamera != null) {  
            if (Integer.parseInt(Build.VERSION.SDK) >= 8)  
                VideoCameraNew2.reconnect(mCamera);  
            mCamera.release();  
            mCamera = null;  
      }  
      mMediaRecorder.release();  
      mMediaRecorder = null;  
    }  
  }  
      
  public void onError(MediaRecorder mr, int what, int extra) {  
    if (what == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) {  
      finish();  
    }  
  }
Copy after login
private void releaseMediaRecorder() { 
    Log.v(TAG, "Releasing media recorder."); 
    if (mMediaRecorder != null) { 
      mMediaRecorder.reset(); 
      if (mCamera != null) { 
            if (Integer.parseInt(Build.VERSION.SDK) >= 8) 
                VideoCameraNew2.reconnect(mCamera); 
            mCamera.release(); 
            mCamera = null; 
      } 
      mMediaRecorder.release(); 
      mMediaRecorder = null; 
    } 
  } 
     
  public void onError(MediaRecorder mr, int what, int extra) { 
    if (what == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) { 
      finish(); 
    } 
  }
Copy after login
private void stopVideoRecording() {  
    Log.v(TAG, "stopVideoRecording");  
    if (mMediaRecorderRecording || mMediaRecorder != null) {  
        Receiver.listener_video = null;  
        t.interrupt();  
      RtpStreamSender.delay = 0;  
 
      if (mMediaRecorderRecording && mMediaRecorder != null) {  
        try {  
          mMediaRecorder.setOnErrorListener(null);  
          mMediaRecorder.setOnInfoListener(null);  
          mMediaRecorder.stop();  
        } catch (RuntimeException e) {  
          Log.e(TAG, "stop fail: " + e.getMessage());  
        }  
 
        mMediaRecorderRecording = false;  
      }  
      releaseMediaRecorder();  
    }  
  }
Copy after login

The above is the detailed content of How to initialize MediaRecorder using Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template