This is a bit long but has useful info about what is actually going wrong when a video plays back in the wrong orientation. Get a nice drink and read on...
Original questions
How can I force the new Intent(MediaStore.ACTION_VIDEO_CAPTURE) to use the front or back camera?
As stated elsewhere, there is an (unreliable) method to ask a camera or video activity to open in either front camera or rear camera mode.
Besides the unreliability of this method, there is nothing about it that will prevent the user from switching front/rear mode after the camera opens, so it will never be a good solution to the problem of upside-down video playback.
How can I know in onActivityResult(..) what camera was used, front or back?
There is no way that I know of. But...
Front vs. back camera is not really the problem anyways.
The underlying problem is that, with some media players, a video captured by an android device may play back in the wrong orientation. This is because, according to the MediaRecorder docs, setting an orientation hint
... will not trigger the source video frame to rotate during video recording, but to add a composition matrix containing the rotation angle in the output video...
And unfortunately:
Some video players may choose to ignore the compostion matrix in a video during playback.
Reading video orientation
Although some media players ignore the orientation hint, your own code can read it.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_VIDEO_CAPTURE) {
Uri vid = data.getData();
Log.i("xcode", "Video captured: " + data.getData());
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(this, vid);
String foo = mmr
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
Log.i("xcode", "Video captured: " + data.getData() + " rotation: "
+ foo);
}
}
(Note that in order to read metadata from the returned video, you will need to provide the MediaStore.EXTRA_OUTPUT
field when you request it, as recommended by the Camera docs.)
When I run this code, I get the following results:
- Rear camera, portrait, rotation == 90
- Front camera, portrait, rotation == 270
- Rear camera, landscape, rotation == 180
- Front camera, landscape, rotation == 180
It looks like these video captures can be problematic for some players when the rotation is 270, so those are the videos you should select out to be fixed by post-processing.