问题描述
我测试样品E:\ Android_SDK \样本\在我使用的是Android工作室,我碰到下面的错误,为什么实际设备的android-22 \媒体\ MediaRecorder?是否有一些错误的样品?
I test the sample E:\Android_SDK\samples\android-22\media\MediaRecorder in my real device using Android Studio, I get the following error, why? Is there some bugs in the sample?
顺便说一句,我的Android优化版本是5.1
BTW, my android verion is 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)
添加
多,效果很好后,我删除安卓?screenOrientation =景观
在AndroidManifest.xml中,但我不知道为什么。
And more, it works well after I remove android:screenOrientation="landscape"
in AndroidManifest.xml, but I don't know why?
推荐答案
MediaRecorder
样品工程风景
模式,并使它在人像工作
模式,以及你需要添加的确切顺序!
MediaRecorder
sample works in Landscape
mode, and to make it work in portrait
mode as well you need to add following code in exact sequence!
这里是code: -
here is the code:-
添加此为人像
支持 mCamera
mCamera.setDisplayOrientation(90);
和支持音频/视频
连接$ C $铬, OUTPUTFORMAT
添加此
and to support Audio/Video
encoder and OutputFormat
add this
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"
这上面的code将工作在两个定位
,但摄像机
显示始终将 90度
,所以你需要通过正确地处理您的方向做出改变。
this above code will work in both orientation
but Camera
Display always will be 90 degree
so you need to make changes by handling your orientation properly.
这篇关于为什么我得到致命异常当我测试MediaRecorder样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!