gpt4 book ai didi

android - 为什么我在测试 MediaRecorder 示例时出现致命异常?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:36:16 24 4
gpt4 key购买 nike

我在真实设备上使用 Android Studio 测试示例 E:\Android_SDK\samples\android-22\media\MediaRecorder,出现以下错误,为什么?示例中是否存在一些错误?

顺便说一句,我的安卓版本是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)

已添加

还有,我把AndroidManifest.xml中的android:screenOrientation="landscape"去掉后效果很好,但不知道为什么?

最佳答案

MediaRecorder样本作品在 Landscape模式,并使其在 portrait 中工作模式以及您需要按确切顺序添加以下代码!

这是代码:-

portrait 添加这个支持mCamera

mCamera.setDisplayOrientation(90);

并支持Audio/Video编码器和 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来自 list

android:screenOrientation="landscape"

上面的代码在 orientation 中都有效。但是Camera显示始终为 90 degree所以你需要通过正确处理你的方向来做出改变。

关于android - 为什么我在测试 MediaRecorder 示例时出现致命异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32818562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com