I created my own activity in my Android application to record videos, and it seems to work very well: the videos are saved in the specified folder on the phone, when I view them in the gallery, they have the correct orientation, and they even upload to the server amazon s3 no problem.
The only problem arises when I watch videos from the server, they are incorrectly rotated depending on the position that I recorded.
I set a video orientation hint when I record them, which allows me to play correctly on my device, but does not affect the downloaded video.
Here is the code that I use to record video
private boolean prepareVideoRecorder() {
if (camera == null)
{
camera = getCameraInstance();
}
if (recorder == null){
recorder = new MediaRecorder();
}
camera.stopPreview();
camera.unlock();
recorder.setCamera(camera);
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
recorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).getAbsolutePath());
recorder.setMaxDuration(50000);
recorder.setVideoFrameRate(30);
recorder.setVideoEncodingBitRate(3000000);
recorder.setAudioEncodingBitRate(8000);
recorder.setPreviewDisplay(cameraPreview.getHolder().getSurface());
int degree = 0;
switch (mOrientation) {
case ORIENTATION_LANDSCAPE_INVERTED:
degree = 180;
break;
case ORIENTATION_PORTRAIT_NORMAL:
degree = 90;
break;
case ORIENTATION_LANDSCAPE_NORMAL:
degree = 0;
break;
case ORIENTATION_PORTRAIT_INVERTED:
degree = 270;
break;
}
recorder.setOrientationHint(degree);
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
e.printStackTrace();
return false;
}
return true;
}
Then start recording:
private void startRecording(){
if (prepareVideoRecorder()) {
timerView.setKeepScreenOn(true);
timerView.setVisibility(View.VISIBLE);
recorder.start();
shutter.setBackgroundResource(R.drawable.custom_button_red);
isRecording = true;
startTimer();
} else {
releaseMediaRecorder();
}
}
And to download:
@Override
protected HttpEntity doInBackground(Object... params){
DefaultHttpClient client = new DefaultHttpClient();
String stepID = (String)params[0];
filepath = (String)params[1];
String auth_token = (String)params[2];
String projectID = (String)params[3];
String filename = Uri.parse(filepath).getLastPathSegment().toString();
String videoType = filename.substring(filename.lastIndexOf("."), filename.length());
String url = VIDEO_URL+"?auth_token=" + auth_token;
HttpPost post = new HttpPost(url);
HttpContext localContext = new BasicHttpContext();
MultipartEntity imageMPentity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
try{
imageMPentity.addPart("project_id", new StringBody(""+projectID));
imageMPentity.addPart("step_id", new StringBody(stepID));
imageMPentity.addPart("content_type", new StringBody("video/"+videoType));
imageMPentity.addPart("filename", new StringBody(filename));
imageMPentity.addPart("video_path", new FileBody(new File(filepath)));
post.setEntity(imageMPentity);
} catch(Exception e){
Log.e(StepDisplay.class.getName(), e.getLocalizedMessage(), e);
}
HttpResponse response = null;
try {
response = client.execute(post, localContext);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity result = null;
if(response != null){
result = response.getEntity();
}
return result;
}
, , , , , . , - .
rails , . , - , , , . MultipartEntity , .