最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決Android MediaRecorder錄制視頻過短問題

 更新時(shí)間:2016年07月21日 14:40:30   投稿:lqh  
本文主要介紹Android MediaRecorder,在使用MediaRecorder時(shí)經(jīng)常會(huì)遇到視頻錄制太短問題,這里提供解決問題的實(shí)例代碼以供大家參考

具體表現(xiàn):

  調(diào)用MediaRecorder的start()與stop()間隔不能小于1秒(有時(shí)候大于1秒也崩),否則必崩。

 錯(cuò)誤信息:

java.lang.RuntimeException: stop failed.
  at android.media.MediaRecorder.stop(Native Method)

 解決辦法:

  在stop以前調(diào)用setOnErrorListener(null);就行了!

 相關(guān)代碼:

 /** 開始錄制 */
  @Override
  public MediaPart startRecord() {
    if (mMediaObject != null && mSurfaceHolder != null && !mRecording) {
      MediaPart result = mMediaObject.buildMediaPart(mCameraId, ".mp4");

      try {
        if (mMediaRecorder == null) {
          mMediaRecorder = new MediaRecorder();
          mMediaRecorder.setOnErrorListener(this);
        } else {
          mMediaRecorder.reset();
        }

        // Step 1: Unlock and set camera to MediaRecorder
        camera.unlock();
        mMediaRecorder.setCamera(camera);
        mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());

        // Step 2: Set sources
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//before setOutputFormat()
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//before setOutputFormat()

        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

        //設(shè)置視頻輸出的格式和編碼
        CamcorderProfile mProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
        //        mMediaRecorder.setProfile(mProfile);
        mMediaRecorder.setVideoSize(640, 480);//after setVideoSource(),after setOutFormat()
        mMediaRecorder.setAudioEncodingBitRate(44100);
        if (mProfile.videoBitRate > 2 * 1024 * 1024)
          mMediaRecorder.setVideoEncodingBitRate(2 * 1024 * 1024);
        else
          mMediaRecorder.setVideoEncodingBitRate(mProfile.videoBitRate);
        mMediaRecorder.setVideoFrameRate(mProfile.videoFrameRate);//after setVideoSource(),after setOutFormat()

        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);//after setOutputFormat()
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);//after setOutputFormat()

        //mMediaRecorder.setVideoEncodingBitRate(800);

        // Step 4: Set output file
        mMediaRecorder.setOutputFile(result.mediaPath);

        // Step 5: Set the preview output
        //        mMediaRecorder.setOrientationHint(90);//加了HTC的手機(jī)會(huì)有問題

        Log.e("Yixia", "OutputFile:" + result.mediaPath);

        mMediaRecorder.prepare();
        mMediaRecorder.start();
        mRecording = true;
        return result;
      } catch (IllegalStateException e) {
        e.printStackTrace();
        Log.e("Yixia", "startRecord", e);
      } catch (IOException e) {
        e.printStackTrace();
        Log.e("Yixia", "startRecord", e);
      } catch (Exception e) {
        e.printStackTrace();
        Log.e("Yixia", "startRecord", e);
      }
    }
    return null;
  }

  /** 停止錄制 */
  @Override
  public void stopRecord() {
    long endTime = System.currentTimeMillis();
    if (mMediaRecorder != null) {
      //設(shè)置后不會(huì)崩
      mMediaRecorder.setOnErrorListener(null);
      mMediaRecorder.setPreviewDisplay(null);
      try {
        mMediaRecorder.stop();
      } catch (IllegalStateException e) {
        Log.w("Yixia", "stopRecord", e);
      } catch (RuntimeException e) {
        Log.w("Yixia", "stopRecord", e);
      } catch (Exception e) {
        Log.w("Yixia", "stopRecord", e);
      }
    }

    if (camera != null) {
      try {
        camera.lock();
      } catch (RuntimeException e) {
        Log.e("Yixia", "stopRecord", e);
      }
    }

    mRecording = false;
  }

  /** 釋放資源 */
  @Override
  public void release() {
    super.release();
    if (mMediaRecorder != null) {
      mMediaRecorder.setOnErrorListener(null);
      try {
        mMediaRecorder.release();
      } catch (IllegalStateException e) {
        Log.w("Yixia", "stopRecord", e);
      } catch (Exception e) {
        Log.w("Yixia", "stopRecord", e);
      }
    }
    mMediaRecorder = null;
  }

  @Override
  public void onError(MediaRecorder mr, int what, int extra) {
    try {
      if (mr != null)
        mr.reset();
    } catch (IllegalStateException e) {
      Log.w("Yixia", "stopRecord", e);
    } catch (Exception e) {
      Log.w("Yixia", "stopRecord", e);
    }
    if (mOnErrorListener != null)
      mOnErrorListener.onVideoError(what, extra);
  }

以上就是對(duì)Android MediaRecorder 資料整理,后續(xù)繼續(xù)補(bǔ)充,有需要的朋友可以參考下。

相關(guān)文章

最新評(píng)論

临沂市| 当雄县| 前郭尔| 门源| 瑞金市| 富顺县| 榕江县| 夏津县| 靖安县| 武安市| 宁强县| 繁昌县| 湄潭县| 宣武区| 柳河县| 阳高县| 天祝| 九寨沟县| 金塔县| 工布江达县| 大兴区| 宜春市| 沂水县| 桃园市| 富裕县| 邹城市| 龙州县| 剑河县| 农安县| 革吉县| 文登市| 闽侯县| 安义县| 布尔津县| 夹江县| 梅河口市| 清新县| 乌恰县| 尼玛县| 保靖县| 麻栗坡县|