2012-03-11 15 views
6

これはAndroidデバイスからのビデオをMP4形式で記録するために使用したコードです。ファイルは作成されていますが、サイズは0バイトです。私は何が間違っているのか理解していないようです。どんな助けもありがとう。android mediarecoderは空のファイルを保存します

   if(mCamera == null) { 
     mCamera = Camera.open(); 
     mCamera.unlock(); 
    } 

    if(mediaRecorder == null) 
     mediaRecorder = new MediaRecorder(); 


    mediaRecorder.setCamera(mCamera); 

    mediaRecorder.setCamera(mCamera); 
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 

    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 

    mediaRecorder.setMaxDuration(maxDurationInMs); 


    mediaRecorder.setOutputFile("/sdcard/1.mp4"); 

    mediaRecorder.setVideoFrameRate(videoFramesPerSecond); 
    mediaRecorder.setVideoSize(176,144); 

    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
    mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); 

    mediaRecorder.setPreviewDisplay(surface); 

    mediaRecorder.setMaxFileSize(maxFileSizeInBytes); 

       mediaRecorder.prepare();   
    try { 
     mediaRecorder.prepare(); 
    } catch (IllegalStateException e) { 
     // This is thrown if the previous calls are not called with the 
     // proper order 
     e.printStackTrace(); 
    } 

      mediaRecorder.start(); 
+0

を見てください - http://stackoverflow.com/questions/10186503/camera-app- not working/10187932#10187932 –

答えて

3

いいので、私は最終的に自分自身をそれを考え出しました。私はここに記載された方法を使用し、それは適切に働いた。

http://developer.android.com/guide/topics/media/camera.html#saving-media

+2

こんにちは、私は同じ問題があります。あなたはどう思いましたか? – Saurabh

+3

こんにちは、あなたの問題を解決するコードを投稿してください – Shalini

+1

うん、それを投稿してください! – Lichtamberg

0
public static final String rootDir = getStorageDir().concat(yours_dir); 
... 
mediaRecorder.setOutputFile(rootDir); 
+0

何らかの理由でgetStorageDir()が検出されません。私は使用しようとしました final String rootDir = Environment.getExternalStorageDirectory()。toString()。concat( "Pictures/i.mp4"); mediaRecorder.setOutputFile(rootDir); まだ動作しません。 –

7

そのような行動の理由は、おそらくあなたは(おそらくrecorder.stop()後)録音を終えた後に再度recorder.setOutputFile()を呼び出す(必ず95%)であるということです。したがって、古いファイルは新しい空のファイルによって書き換えられています。

0

あなたは以下のコードは、私のために働いたユーザー権限

<uses-permission android:name="android.permission.CAMERA" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.RECORD_AUDIO" /> 
2

を設定しています:

private boolean isRecording = false; 

public static final int MEDIA_TYPE_IMAGE = 1; 
public static final int MEDIA_TYPE_VIDEO = 2; 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_video_record); 

    // Create an instance of Camera 
    mCamera = getCameraInstance(); 

    mHolder = surfaceViewRecReadyTestimonial.getHolder(); 
    mHolder.addCallback(this); 
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
} 


public static Camera getCameraInstance() { 
    Camera c = null; 
    try { 
     c = Camera.open(); // attempt to get a Camera instance 
    } catch (Exception e) { 
     // Camera is not available (in use or does not exist) 
    } 
    return c; // returns null if camera is unavailable 
} 

private boolean prepareVideoRecorder() { 

    mCamera = getCameraInstance(); 
    mMediaRecorder = new MediaRecorder(); 

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

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

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) 
    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)); 

    // Step 4: Set output file 
    mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString()); 

    // Step 5: Set the preview output 
    mMediaRecorder.setPreviewDisplay(surfaceViewRecReadyTestimonial.getHolder().getSurface()); 

    // Step 6: Prepare configured MediaRecorder 
    try { 
     mMediaRecorder.prepare(); 
    } catch (IllegalStateException e) { 
     Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage()); 
     releaseMediaRecorder(); 
     return false; 
    } catch (IOException e) { 
     Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage()); 
     releaseMediaRecorder(); 
     return false; 
    } 
    return true; 
} 

@Override 
public void onClick(View v) { 

    if (v == btnStart) { 

      // initialize video camera 
      if (prepareVideoRecorder()) { 
       // Camera is available and unlocked, MediaRecorder is prepared, 
       // now you can start recording 
       mMediaRecorder.start(); 

       // inform the user that recording has started 
       isRecording = true; 
      } else { 
       // prepare didn't work, release the camera 
       releaseMediaRecorder(); 
       // inform user 
      } 


     } else if (v == btnStop) { 

      if (isRecording) { 
       // stop recording and release camera 
       mMediaRecorder.stop(); // stop the recording 
       releaseMediaRecorder(); // release the MediaRecorder object 
       mCamera.lock();   // take camera access back from MediaRecorder 

       // inform the user that recording has stopped 
       isRecording = false; 
      } 

     } } 

private void releaseMediaRecorder() { 
    if (mMediaRecorder != null) { 
     mMediaRecorder.reset(); // clear recorder configuration 
     mMediaRecorder.release(); // release the recorder object 
     mMediaRecorder = null; 
     mCamera.lock();   // lock camera for later use 
    } 
} 

private void releaseCamera() { 
    if (mCamera != null) { 
     mCamera.release();  // release the camera for other applications 
     mCamera = null; 
    } 
} 

/** 
* Create a File for saving an image or video 
*/ 
private static File getOutputMediaFile(int type) { 
    // To be safe, you should check that the SDCard is mounted 
    // using Environment.getExternalStorageState() before doing this. 

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_DCIM), "FolderName"); 
    // This location works best if you want the created images to be shared 
    // between applications and persist after your app has been uninstalled. 

    // Create the storage directory if it does not exist 
    if (!mediaStorageDir.exists()) { 
     if (!mediaStorageDir.mkdirs()) { 
      Log.d("FolderName", "failed to create directory"); 
      return null; 
     } 
    } 

    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    File mediaFile; 
    if (type == MEDIA_TYPE_IMAGE) { 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
       "IMG_" + timeStamp + ".jpg"); 
    } else if (type == MEDIA_TYPE_VIDEO) { 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
       "VID_" + timeStamp + ".mp4"); 
    } else { 
     return null; 
    } 

    return mediaFile; 
} 

@Override 
protected void onPause() { 
    super.onPause(); 

    releaseMediaRecorder();  // if you are using MediaRecorder, release it first 
    releaseCamera();    // release the camera immediately on pause event 

} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    // The Surface has been created, now tell the camera where to draw the preview. 
    try { 
     mCamera.setPreviewDisplay(holder); 
     mCamera.startPreview(); 
    } catch (IOException e) { 
     Log.d("RecordVideo", "Error setting camera preview: " + e.getMessage()); 
    } 
} 

@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 

    // If your preview can change or rotate, take care of those events here. 
    // Make sure to stop the preview before resizing or reformatting it. 

    if (mHolder.getSurface() == null) { 
     // preview surface does not exist 
     return; 
    } 

    // stop preview before making changes 
    try { 
     mCamera.stopPreview(); 
    } catch (Exception e) { 
     // ignore: tried to stop a non-existent preview 
    } 

    try { 
     Camera.Parameters parameters = mCamera.getParameters(); 

     Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 

     if (display.getRotation() == Surface.ROTATION_0) { 
      parameters.setPreviewSize(height, width); 
      mCamera.setDisplayOrientation(90); 
     } 

     if (display.getRotation() == Surface.ROTATION_90) { 
      parameters.setPreviewSize(width, height); 
     } 

     if (display.getRotation() == Surface.ROTATION_180) { 
      parameters.setPreviewSize(height, width); 
     } 

     if (display.getRotation() == Surface.ROTATION_270) { 
      parameters.setPreviewSize(width, height); 
      mCamera.setDisplayOrientation(180); 
     } 

     if (mCamera != null) { 
      try { 
       mCamera.setPreviewDisplay(mHolder); 
       mCamera.startPreview(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    // set preview size and make any resize, rotate or 
    // reformatting changes here 

    // start preview with new settings 
    try { 
     mCamera.setPreviewDisplay(mHolder); 
     mCamera.startPreview(); 

    } catch (Exception e) { 
     Log.d(TAG, "Error starting camera preview: " + e.getMessage()); 
    } 

} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 

} 
関連する問題