1

Simple working code:

m_TakeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (m_TakeVideoIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
    startActivityForResult(m_TakeVideoIntent, REQUEST_VIDEO_CAPTURE);
}

// and when done:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_VIDEO_CAPTURE) {
            // code
        } else {
            // other code
        }
    }
}

Question:

How can I know in onActivityResult(..) what camera was used, front or back ?

OR

How can I force the new Intent(MediaStore.ACTION_VIDEO_CAPTURE) to use the front or back camera ?

(This is because when using the front camera - the result video plays upside down.)

Miko Diko
  • 944
  • 1
  • 13
  • 33

2 Answers2

3

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.

x-code
  • 2,940
  • 1
  • 18
  • 19
  • Thanks x-code, this code seems to work, I hope it will also work when using an already ready (existing) video (not through a run-time video capturing activity). – Miko Diko Oct 13 '14 at 07:51
  • This does not work for an existing video, it always returns "0". Is there any way to know the rotation of an existing video ? – Miko Diko Oct 14 '14 at 08:10
  • Where does this existing video come from? What format is it in? Does it play in the wrong orientation? – x-code Oct 14 '14 at 09:47
  • The video I tested came from the default camera capture "app" the smart-phone came with.. the same way it's captured from within my app, but started it without the app ofcourse. so I think it's the same format. yes wrong orientation, used the front camera, the video is playing up-side-down, but unlike videos captured through my app, the code you posted returns "0" instead of "270" like it does when starting an Intent through the app. – Miko Diko Oct 14 '14 at 14:03
  • Offhand I don't know why there would be a difference between those two cases. – x-code Oct 14 '14 at 17:41
1

See https://stackoverflow.com/a/11159760/192373 - it uses an undocumented extra to control which camera will be opened.

Please note that the intent may be served by different apps, depending on the device, ROM, and the third-party apps the end user chose to install and activate.

For image capture, you have a chance to discover the camera info afterwise, but not for video.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Thanks Alex, but I've seen this before, it didn't work on a Galaxy S5, that's why I've made this new question :) – Miko Diko Oct 13 '14 at 05:44