0

I only need to record a video in Android device with some specifications. However, i'm really disappointed because everything that i try some weird system issue appeared and i go back to beginning. So i need a more experienced developer to guide me.

Specifications

I need the following:

  • Basic video interface (user seeing what is recorded).
  • Video time cannot exceed 20s (Max:20s after that recording stops).
  • Only Landscape mode is enable. Block user somehow so he can't record in portrait mode.
  • Configuration button should be enabled. Setting button that allow user to change video resolution
  • Front and back camera button should be enabled.
  • As many as possible, devices must be compatible with my application.

What i already tried

The below code was my first try. I was happy because it works, but it can't block portrait recording videos. So i read from some other question here that i should use camera2 android API for that.

Uri videoUri = FileProvider.getUriForFile(this, "com.my.myapplication.fileprovider", videofile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, VIDEO_DURATION);
intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // this won't work
intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_LOCKED); // neither this
startActivityForResult(intent, REQUEST_VIDEO_CAPTURE);

So i got my 2nd try. I changed my code to this camera2 API just to know that most of Samsung devices don't support it. Here is my source to that information. Of course, i just discovered that after some weird mistakes appeared in my code.

I followed this code: androidwave code

And this code: lytcom/Camera2Kit

And this code: googlearchive/android-Camera2Video

Which are pretty most the same.

Since devices aren't compatible with camera2 API, they won't return good camera video size. I know that my device has a 1920x1080 resolution camera but the return from system method is

E/Camera2VideoFragment: Width: 1440 Height 1080
E/Camera2VideoFragment: Width: 1088 Height 1088
E/Camera2VideoFragment: Width: 1472 Height 720
E/Camera2VideoFragment: Width: 1280 Height 720
E/Camera2VideoFragment: Width: 1056 Height 864
E/Camera2VideoFragment: Width: 960 Height 720

My third try. I was using the library JeroenMols/LandscapeVideoCamera that gives me exactly what i need, including this beautiful picture that shows user to rotate device before recording. However this library won't work for API>22, source.

Currently

NOW, i don't know what to do. Whatever i tried didn't work. And most answer around just used those libraries. So can please somebody lend me a hand? Please Java language. I can't handle Kotlin.

BoredDev
  • 23
  • 3

1 Answers1

0

Since i'm in a rush, i solved my problem with JeroenMols/LandscapeVideoCamera library. Maybe because of Android version incompatibility i will need to change later. Despite that, i will write my answer here, maybe will help someone else.

The main problem when using this library with new android version (>=Android.M) is the runtime permissions. Besides adding those permissions to the manifest, i requested all necessary permissions in runtime: Camera, Audio and Storage write.

Code for LandscapeVideoCamera

void startCamera(){

            CaptureConfiguration.Builder builder = new CaptureConfiguration.Builder(width, height, bitrate);
            builder.maxDuration(20);
            builder.frameRate(24);
            builder.showRecordingTime();
            CaptureConfiguration configuration = builder.build();
            final Intent intent = new Intent(this, VideoCaptureActivity.class);
            intent.putExtra(VideoCaptureActivity.EXTRA_CAPTURE_CONFIGURATION, configuration);
            intent.putExtra(VideoCaptureActivity.EXTRA_OUTPUT_FILENAME, videofile.getAbsolutePath());
            startActivityForResult(intent, REQUEST_VIDEO_CAPTURE);
}

Asking Permissions

    public void launchCamera(View v){
         //first test if SDK is Marshmallow

            // if it's then we need to ask permission during run time
            //first check if we already got permission
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                requestStoragePermission();
                return;
            }
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) {
                requestCameraPermission();
                return;
            }
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO)
                    != PackageManager.PERMISSION_GRANTED) {
                requestAudioRecordPermission();
                return;
            }
            startCamera();
        }
    }

Request Permission Result

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if(requestCode == STORAGE_PERMISSION_CODE){
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){               
                // permission given
                startCamera();
            }
            else{
                // permission not given
            }
        }
        if(requestCode == CAMERA_PERMISSION_CODE){
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                // permission given
                startCamera();
            }
            else{
                // permission not given
            }
        }
        if(requestCode == AUDIO_PERMISSION_CODE){
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){                
                startCamera();
            }
            else{
                // permission not given
            }
        }
    }
BoredDev
  • 23
  • 3