纯净、安全、绿色的下载网站

首页|软件分类|下载排行|最新软件|IT学院

当前位置:首页IT学院IT技术

java MediaRecorder java中初始化MediaRecorder的实现方法

csguo007   2021-03-28 我要评论
想了解java中初始化MediaRecorder的实现方法的相关内容吗,csguo007在本文为您仔细讲解java MediaRecorder的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java中初始化MediaRecorder的方法,java,MediaRecorder的使用,下面大家一起来学习吧。

java中初始化MediaRecorder

实现代码:

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;  
  } 
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; 
  } 
 
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();  
    }  
  } 
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(); 
    } 
  } 
 
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();  
    }  
  } 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


相关文章

猜您喜欢

  • bootstrap 标题设置跨行无效 BootStrap 标题设置跨行无效的解决办法

    想了解BootStrap 标题设置跨行无效的解决办法的相关内容吗,SunShine_2017在本文为您仔细讲解bootstrap 标题设置跨行无效的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:bootstrap,标题设置跨行无效,下面大家一起来学习吧。..
  • Hadoop Data Locality Hadoop上Data Locality的详解

    想了解Hadoop上Data Locality的详解的相关内容吗,csguo007在本文为您仔细讲解 Hadoop Data Locality的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Hadoop上Data,Locality的详解,Hadoop上Data,Locality的使用,下面大家一起来学习吧。..

网友评论

Copyright 2020 www.9aisf.com 【就爱软件站】 版权所有 软件发布

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 点此查看联系方式