Why did I get FATAL EXCEPTION when I tested the MediaRecorder sample?

I am testing sample E: \ Android_SDK \ samples \ android-22 \ media \ MediaRecorder on my real device using Android Studio, I get the following error, why? Are there any errors in the sample?

By the way, my android verion 5.1

09-28 16:09:31.683  17233-17233/com.example.android.mediarecorder E/Zygote﹕ v2
09-28 16:09:31.683  17233-17233/com.example.android.mediarecorder E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
09-28 16:10:06.343  17233-17772/com.example.android.mediarecorder E/MediaRecorder﹕ start failed: -19
09-28 16:10:06.343  17233-17772/com.example.android.mediarecorder E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.example.android.mediarecorder, PID: 17233
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:304)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.RuntimeException: start failed.
            at android.media.MediaRecorder.start(Native Method)
            at com.example.android.mediarecorder.MainActivity$MediaPrepareTask.doInBackground(MainActivity.java:208)
            at com.example.android.mediarecorder.MainActivity$MediaPrepareTask.doInBackground(MainActivity.java:200)
            at android.os.AsyncTask$2.call(AsyncTask.java:292)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

Added

And yet, it works fine after uninstalling android:screenOrientation="landscape"in AndroidManifest.xml, but I don’t know why?

+4
source share
3 answers

MediaRecorderThe sample works in mode Landscape, and for it to work in mode portrait, you need to add the following code in the exact sequence !

here is the code: -

add this for support portraitinmCamera

mCamera.setDisplayOrientation(90);

Audio/Video encoder OutputFormat

            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

prepareVideoRecorder

private boolean prepareVideoRecorder(){

        // BEGIN_INCLUDE (configure_preview)
        mCamera = CameraHelper.getDefaultCameraInstance();
        mCamera.setDisplayOrientation(90);
        // We need to make sure that our preview and recording video size are supported by the
        // camera. Query camera to find all the sizes and choose the optimal size given the


        try {
                // Requires API level 11+, For backward compatibility use {@link setPreviewDisplay}
                // with {@link SurfaceView}
                mCamera.setPreviewTexture(mPreview.getSurfaceTexture());

        } catch (IOException e) {
            Log.e(TAG, "Surface texture is unavailable or unsuitable" + e.getMessage());
            return false;
        }
        // END_INCLUDE (configure_preview)


        // BEGIN_INCLUDE (configure_media_recorder)
        mMediaRecorder = new MediaRecorder();

        // Step 1: Unlock and set camera to MediaRecorder
        mCamera.unlock();
        mMediaRecorder.setCamera(mCamera);

        // Step 2: Set sources
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);

        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        mMediaRecorder.setOrientationHint(90);

        // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)

        mMediaRecorder.setOutputFile(CameraHelper.getOutputMediaFile(
                CameraHelper.MEDIA_TYPE_VIDEO).toString());
        //
        //mMediaRecorder.setPreviewDisplay(SufaceView);
        // Step 4: Set output file

        // END_INCLUDE (configure_media_recorder)

        // Step 5: 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;
    }

, , screenOrientation

android:screenOrientation="landscape"

orientation, Camera 90 degree, , .

+3

MediaRecorder, "android:theme="@android:style/Theme.NoTitleBar.Fullscreen", .

<activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >>
+2

android:screenOrientation="landscape" , , .

Please make sure you are using the latest version of Sample projects.

Or open the project with

File> New> Import Type> MediaRecorder Type

or

git clone https://github.com/googlesamples/android-MediaRecorder.git

I just tested the sample code with Android 5.1 (API level 22), and it worked perfectly on both the tablet and the smartphone.

Save android:theme="@style/AppTheme"in the "Application" section and do not change it, as this is not what the sample code provides. Template styles are defined in

res> values> template-styles.xml

and it is defined for both the tablet and the smartphone.

+1
source

Source: https://habr.com/ru/post/1609256/


All Articles