2016-11-26 65 views
0

我想提出一個自定義視頻錄像機來記錄video.Here 4是我的代碼MediaRecorder:啓動名爲處於無效狀態:在錄像

boolean usecamera = true; 
    boolean previewRunning = false; 
    SurfaceView surfaceView; 
    Button btnStart, btnStop; 
    File root; 
    File file; 
    Boolean isSDPresent; 
    SimpleDateFormat simpleDateFormat; 
    String timeStamp; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
     setContentView(R.layout.activity_surface); 
     initComs(); 
     actionListener(); 
    } 

    private void initComs() { 
     simpleDateFormat = new SimpleDateFormat("ddMMyyyyhhmmss"); 
     timeStamp = simpleDateFormat.format(new Date()); 
     camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); 
     surfaceView = (SurfaceView) findViewById(R.id.preview); 
     surfaceHolder = surfaceView.getHolder(); 
     surfaceHolder.addCallback(this); 
     btnStop = (Button) findViewById(R.id.btn_stop); 
     surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
     isSDPresent = android.os.Environment.getExternalStorageState().equals(
       android.os.Environment.MEDIA_MOUNTED); 

    } 

    public static float megabytesAvailable(File f) { 
     StatFs stat = new StatFs(f.getPath()); 
     long bytesAvailable = (long) stat.getBlockSize() 
       * (long) stat.getAvailableBlocks(); 
     return bytesAvailable/(1024.f * 1024.f); 
    } 



    private void actionListener() { 
     btnStop.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (recording) { 
        recorder.stop(); 
        if (usecamera) { 
         try { 
          camera.reconnect(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
        } 
        // recorder.release(); 
        recording = false; 
        // Let's prepareRecorder so we can record again 
        prepareRecorder(); 
       } 

      } 
     }); 
    } 

    private void prepareRecorder() { 
     recorder = new MediaRecorder(); 
     recorder.setPreviewDisplay(surfaceHolder.getSurface()); 
     if (usecamera) { 
      camera.unlock(); 
      recorder.setCamera(camera); 
     } 
     recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); 
     recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); 

     recorder.setProfile(camcorderProfile); 



     if (camcorderProfile.fileFormat == MediaRecorder.OutputFormat.MPEG_4) { 
      recorder.setOutputFile("/sdcard/XYZApp/" + "XYZAppVideo" + "" 
        + new SimpleDateFormat("ddMMyyyyHHmmss").format(new Date()) 
        + ".mp4"); 
     } else if (camcorderProfile.fileFormat == MediaRecorder.OutputFormat.MPEG_4) { 
      recorder.setOutputFile("/sdcard/XYZApp/" + "XYZAppVideo" + "" 
        + new SimpleDateFormat("ddMMyyyyHHmmss").format(new Date()) 
        + ".mp4"); 
     } else { 
      recorder.setOutputFile("/sdcard/XYZApp/" + "XYZAppVideo" + "" 
        + new SimpleDateFormat("ddMMyyyyHHmmss").format(new Date()) 
        + ".mp4"); 
     } 

     try { 
      recorder.prepare(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
      finish(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      finish(); 
     } 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
     System.out.println("onsurfacecreated"); 

     if (usecamera) { 
      camera = Camera.open(); 

      try { 
       camera.setPreviewDisplay(holder); 
       camera.startPreview(); 
       previewRunning = true; 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 

    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
           int height) { 
     System.out.println("onsurface changed"); 

     if (!recording && usecamera) { 
      if (previewRunning) { 
       camera.stopPreview(); 
      } 

      try { 
       Camera.Parameters p = camera.getParameters(); 

       p.setPreviewSize(camcorderProfile.videoFrameWidth, 
         camcorderProfile.videoFrameHeight); 
       p.setPreviewFrameRate(camcorderProfile.videoFrameRate); 

       camera.setParameters(p); 

       camera.setPreviewDisplay(holder); 
       camera.startPreview(); 
       previewRunning = true; 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      prepareRecorder(); 
      if (!recording) { 
       recording = true; 
       recorder.start(); 
      } 
     } 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     if (recording) { 
      recorder.stop(); 
      recording = false; 
     } 
     recorder.release(); 
     if (usecamera) { 
      previewRunning = false; 
      // camera.lock(); 
      camera.release(); 
     } 
     finish(); 
    } 
} 

但是,當我運行這段代碼就顯示錯誤MediaRecorder:開始呼叫處於無效狀態:4。我不知道是什麼問題。我試着從我身邊的一切,但沒有任何作品。我是Android新手請幫助我。

+0

請添加錯誤日誌 –

回答

0

請嘗試:

MediaRecorder mMediaRecorder = new MediaRecorder(); 

mMediaRecorder.reset(); 
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION); 
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); 
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); 
mMediaRecorder.setOutputFile(directory_path + path); 
mMediaRecorder.setVideoSize(width, height); 
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);      

mMediaRecorder.setVideoEncodingBitRate(bitrate); 
mMediaRecorder.setVideoFrameRate(framerate); 
int rotation = getWindowManager().getDefaultDisplay().getRotation(); 
int orientation = ORIENTATIONS.get(rotation + 90); 
mMediaRecorder.setOrientationHint(orientation); 
mMediaRecorder.prepare(); 
相關問題