I am trying to implement a function in which I can start and stop video recording several times, and accumulate video data in File .
This is how I prepare my media recorder:
private boolean prepareVideoRecorder(){ mMediaRecorder = new MediaRecorder(); //0 for landscape //90 for portrait //Check for available profile CamcorderProfile profile = null; if(CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_480P)){ Log.d(TAG, "480p"); profile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P); }else if(CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_720P)){ Log.d(TAG, "720p"); profile = CamcorderProfile.get(CamcorderProfile.QUALITY_720P); }else{ Log.d(TAG, "LOW"); profile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW); } // 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 profile mMediaRecorder.setProfile(profile); // Step 4: Set output file and pass media recorder the file descriptor if(mStoreFile == null){ mStoreFile = MediaUtil.getOutputMediaFile(MediaUtil.MEDIA_TYPE_VIDEO); try { mStoreFile.createNewFile(); } catch (Exception e) { e.printStackTrace(); } } try { mOutputStream = new FileOutputStream(mStoreFile, true); mMediaRecorder.setOutputFile(mOutputStream.getFD()); } catch (Exception e1) { e1.printStackTrace(); } mMediaRecorder.setMaxDuration(30000); // Step 5: Set the preview output mMediaRecorder.setPreviewDisplay(mPreviewSurface.getHolder().getSurface()); //Check orientation and set hint switch(mOrientation){ case ORIENTATION_PORTRAIT_NORMAL: mMediaRecorder.setOrientationHint(90); break; case ORIENTATION_PORTRAIT_INVERTED: mMediaRecorder.setOrientationHint(270); break; case ORIENTATION_LANDSCAPE_NORMAL: mMediaRecorder.setOrientationHint(0); break; case ORIENTATION_LANDSCAPE_INVERTED: mMediaRecorder.setOrientationHint(180); break; } // 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; }
This is the button on the click:
@Override public void onClick(View v) { if (isRecording) { try{ mOutputStream.close(); }catch(Exception ee){ } // stop recording and release camera mMediaRecorder.stop(); // stop the recording mMediaRecorder.reset(); mCamera.lock(); // take camera access back from MediaRecorder // inform the user that recording has stopped mRecordButton.setImageResource(R.drawable.record_button); isRecording = false; } else { // 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 mRecordButton.setImageResource(R.drawable.record_button_on); isRecording = true; } else { // prepare didn't work, release the camera releaseMediaRecorder(); // inform user } } } });
MediaUtil.getOutputMediaFile(MediaUtil.MEDIA_TYPE_VIDEO) will give me something like: / storage / sdcard 0 / Pictures / Project / VID_2013.mp4
Current issue:
The way I am testing it at the moment is:
- Start recording
- Stop recording
- Start recording
- stop recording
- Go to the Android file manager on / Pictures / Project path
- I see a file that has been created and "added" with several data segments. But he will not play. And it has no cover, like other video files.
Somewhere along the line the file is damaged? This does not work if I just write once and check the file in the repository. I can record video if I just define the File for MediaRecorder in setOutputFile , but I'm trying to add video data for several shots. Is it possible?
Bundeeteddee
source share