我使用Android Studio在我的真实设备中测试样本E:\ Android_SDK \ samples \ android-22 \ media \ MediaRecorder,我收到以下错误,为什么?样本中是否有一些错误?
顺便说一下,我的android版本是5.109-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"
后效果很好,但我不知道为什么?
答案 0 :(得分:3)
MediaRecorder
示例适用于Landscape
模式,并且要使其在portrait
模式下运行,您还需要在完整序列中添加以下代码!< / p>
这是代码: -
将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
android:screenOrientation="landscape"
上述代码同时适用于orientation
但Camera
显示始终为90 degree
,因此您需要通过正确处理方向进行更改。
答案 1 :(得分:2)
我也面临上述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 :(得分:1)
理想情况下,您应该在活动中包含android:screenOrientation="landscape"
,这告诉我您手中有一个过时的示例项目。
请确保您运行的是最新版本的Sample项目。
通过
打开项目档案&gt;新&gt;导入样本&gt;输入MediaRecorder
或
git clone https://github.com/googlesamples/android-MediaRecorder.git
刚刚使用Android 5.1(API级别22)测试了示例代码,它在平板电脑和智能手机上都运行良好。
请将android:theme="@style/AppTheme"
保留在“应用程序”下,并且不要对其进行更改,因为这不是示例代码提供的内容。模板样式在
res&gt;值&gt;模板styles.xml
并且它适用于平板电脑和智能手机。
答案 3 :(得分:0)
缺少权限
在MainActivity.java的onCreate中添加此调用
if (!checkPermissionFromDevice())
requestPermission();
并将这两个方法插入MainActivity.java
private boolean checkPermissionFromDevice()
{
int write_external_storage_result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int camera_result = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
int record_audio_result = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO);
return write_external_storage_result == PackageManager.PERMISSION_GRANTED
&& camera_result == PackageManager.PERMISSION_GRANTED
&& record_audio_result == PackageManager.PERMISSION_GRANTED;
}
private void requestPermission()
{
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO},
REQUEST_PERMISSION_CODE);
}