在android上捕获视频会导致运行时异常启动失败:-19开-版本4.0.1 galaxy s 2
更新1:将代码更改为以下代码后,现在代码会出现此错误:在android上捕获视频会导致运行时异常启动失败:-19开-版本4.0.1 galaxy s 2,android,android-camera,video-capture,runtimeexception,android-mediarecorder,Android,Android Camera,Video Capture,Runtimeexception,Android Mediarecorder,更新1:将代码更改为以下代码后,现在代码会出现此错误: 02-20 10:43:13.624: W/System.err(5494): java.lang.RuntimeException: start failed. 02-20 10:43:13.624: W/System.err(5494): at android.media.MediaRecorder.start(Native Method) 02-20 10:43:13.624: W/System.err(5494):
02-20 10:43:13.624: W/System.err(5494): java.lang.RuntimeException: start failed.
02-20 10:43:13.624: W/System.err(5494): at android.media.MediaRecorder.start(Native Method)
02-20 10:43:13.624: W/System.err(5494): at fun.audios.maker.VideoRecorderActivity.startRecording(VideoRecorderActivity.java:237)
02-20 10:43:13.624: W/System.err(5494): at fun.audios.maker.VideoRecorderActivity.access$9(VideoRecorderActivity.java:146)
02-20 10:43:13.624: W/System.err(5494): at fun.audios.maker.VideoRecorderActivity$1.onClick(VideoRecorderActivity.java:88)
02-20 10:43:13.624: W/System.err(5494): at android.view.View.performClick(View.java:3620)
02-20 10:43:13.624: W/System.err(5494): at android.widget.CompoundButton.performClick(CompoundButton.java:107)
02-20 10:43:13.624: W/System.err(5494): at android.view.View$PerformClick.run(View.java:14292)
02-20 10:43:13.624: W/System.err(5494): at android.os.Handler.handleCallback(Handler.java:605)
02-20 10:43:13.624: W/System.err(5494): at android.os.Handler.dispatchMessage(Handler.java:92)
02-20 10:43:13.624: W/System.err(5494): at android.os.Looper.loop(Looper.java:137)
02-20 10:43:13.624: W/System.err(5494): at android.app.ActivityThread.main(ActivityThread.java:4512)
02-20 10:43:13.624: W/System.err(5494): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 10:43:13.624: W/System.err(5494): at java.lang.reflect.Method.invoke(Method.java:511)
02-20 10:43:13.624: W/System.err(5494): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
02-20 10:43:13.624: W/System.err(5494): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
02-20 10:43:13.624: W/System.err(5494): at dalvik.system.NativeStart.main(Native Method)
代码更改为:
private void startRecording()
{
mrec = new MediaRecorder();
mCamera.unlock();
mrec.setCamera(mCamera);
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mrec.setVideoFrameRate(15); //this line removed comment
mrec.setVideoSize(720, 480);
Method[] methods = mrec.getClass().getMethods();
for (Method method: methods){
try
{
if(method.getName().equals("setAudioEncodingBitRate"))
{
method.invoke(mrec,12200);
}
else if(method.getName().equals("setVideoEncodingBitRate"))
{
method.invoke(mrec, 3000000);
}
else if(method.getName().equals("setAudioSamplingRate"))
{
method.invoke(mrec,8000);
}
else if(method.getName().equals("setVideoFrameRate"))
{
method.invoke(mrec,24);
}
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
catch (RuntimeException e)
{
Log.d("errorrrrrrrrr runtime111111111", "okkkkkkkkkk");
e.printStackTrace();
}
}
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setOrientationHint(90);
mrec.setOutputFile(Videopath);
try
{
mrec.prepare();
}
catch (IllegalStateException e)
{
// releaseRecorder();
// return false;
}
catch (IOException e)
{
// releaseRecorder();
// return false;
}
catch (RuntimeException e)
{
e.printStackTrace();
}
catch(Exception e)
{
// return false;
}
try
{
mrec.start();
}
catch (RuntimeException e)
{
Log.d("errorrrrrrrrr runtime44444444444", "okkkkkkkkkk");
e.printStackTrace();
}
catch(Exception e)
{
// return false;
Log.d("errorrrrrrrrr 33333333333", "okkkkkkkkkk");
}
isRecording=true;
// return true;
}
permissons添加了如下内容:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.screen.portrait" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>this is my
这是我的xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal" android:background="@drawable/bg_header_footers">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:paddingTop="2dp">
<TextView
android:id="@+id/tvSetNameRecorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/White"
android:textSize="18dp"
android:textStyle="bold"
android:text="Make your Video!"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tvTimer1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:textColor="@color/Lightgray"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"
android:text="00:00:00"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp">
<ImageView
android:id="@+id/imgcheshmak1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/cheshmak"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<SurfaceView
android:id="@+id/surface_camera"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<TableLayout
android:gravity="center_horizontal"
android:id="@+id/linerlistandback"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content">
<TableRow
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dp" android:layout_weight="1" android:paddingRight="2dp">
<Button
android:id="@+id/btnToggleListVideo"
android:layout_width="0dp"
android:layout_height="40dp"
android:background="@drawable/selector_button"
android:drawableLeft="@drawable/listtttt"
android:paddingLeft="30dp"
android:paddingRight="20dp"
android:layout_weight="1"
android:textSize="20dp" android:text="List"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1" android:paddingLeft="2dp">
<ToggleButton
android:id="@+id/toggleRecordingButton"
android:layout_width="0dp"
android:textOff="Start"
android:textOn="Stop"
android:background="@drawable/selector_button"
android:layout_height="40dp"
android:drawableLeft="@drawable/camera"
android:paddingLeft="30dp"
android:paddingRight="20dp" android:layout_weight="1" android:textSize="20dp"/>
</LinearLayout>
</TableRow>
</TableLayout>
</LinearLayout>
看起来您没有使用相机服务的正确权限。 尝试将其添加到清单文件: 编辑:这对我有用!我使用了与您相同的代码,我知道我以前说过,但请检查您的权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.screen.portrait" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
我想知道您是否添加了适当的权限。 您必须在menifest中添加权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
当您使用摄像头时,如果摄像头与其他应用程序连接,则您无法使用摄像头(这可能会导致无法连接摄像头服务)
因此,您必须确保只有您的应用程序使用摄像头服务
我希望你改正你的错误
再见 根据你的事故日志
02-20 05:42:56.054: W/System.err(4991): java.lang.RuntimeException: Fail to connect to camera service
W/System.err(4991): at android.hardware.Camera.native_setup(Native Method)
02-20 05:42:56.054: W/System.err(4991): at android.hardware.Camera.<init>(Camera.java:344)
02-20 05:42:56.054: W/System.err(4991): at android.hardware.Camera.open(Camera.java:321)
02-20 05:42:56.054: W/System.err(4991): at fun.audios.maker.VideoRecorderActivity.startRecording(VideoRecorderActivity.java:144)
如果代码不能工作,那应该是您的ROM问题。我终于找到了解决方案。我删除了代码的一部分,然后启动失败的错误并没有发生。 我将代码更改为:
private void startRecording()
{
mrec = new MediaRecorder();
mCamera.unlock();
mrec.setCamera(mCamera);
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mrec.setVideoFrameRate(15);
mrec.setVideoSize(720, 480);
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setOrientationHint(90);
mrec.setOutputFile(Videopath);
try
{
mrec.prepare();
mrec.start();
}
catch (IllegalStateException e)
{
// releaseRecorder();
// return false;
}
catch (IOException e)
{
// releaseRecorder();
// return false;
}
catch (RuntimeException e)
{
e.printStackTrace();
}
catch(Exception e)
{
// return false;
}
}
我猜invoke方法的参数值在设置时出现问题。如果您有一些更好的响应,例如我的invoke方法,请将您的响应发送给我,以帮助其他人阅读本文。您的清单中是否有正确的权限?是@steven我添加了这些权限:尝试在此处查看@steven这个链接帮不了我。我能做什么@Steven?这对我来说很有必要。抱歉@Steven无意窃取你的评论。我一发布答案就看到了。我有必要的许可。你能发布你的layout.xml吗?那么什么时候会给你一个例外??你能在SurfaceView上看到预览吗?我刚刚在我的HTC One上试用过,它给我展示了预览版……是的,我看得出来。但是,当单击“开始”使相机服务失败时。我有所需的权限。我能做些什么来重新解决我的问题。我现在需要这个。我不想面对前面的相机!我现在也试着告诉你。同样的例外给了我!!我能做些什么@yushulx?这是我的必要条件你能在其他设备上试用你的代码吗?或模拟器?在真实设备上。sumsong版本2.2上的galaxy s 2版本4.0.1在真实设备上正确运行
02-20 05:42:56.054: W/System.err(4991): java.lang.RuntimeException: Fail to connect to camera service
W/System.err(4991): at android.hardware.Camera.native_setup(Native Method)
02-20 05:42:56.054: W/System.err(4991): at android.hardware.Camera.<init>(Camera.java:344)
02-20 05:42:56.054: W/System.err(4991): at android.hardware.Camera.open(Camera.java:321)
02-20 05:42:56.054: W/System.err(4991): at fun.audios.maker.VideoRecorderActivity.startRecording(VideoRecorderActivity.java:144)
private boolean prepareVideoRecorder(){
mCamera = getCameraInstance();
mMediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
// Step 4: Set output file
mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
// Step 6: 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;
}
private void startRecording()
{
mrec = new MediaRecorder();
mCamera.unlock();
mrec.setCamera(mCamera);
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mrec.setVideoFrameRate(15);
mrec.setVideoSize(720, 480);
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setOrientationHint(90);
mrec.setOutputFile(Videopath);
try
{
mrec.prepare();
mrec.start();
}
catch (IllegalStateException e)
{
// releaseRecorder();
// return false;
}
catch (IOException e)
{
// releaseRecorder();
// return false;
}
catch (RuntimeException e)
{
e.printStackTrace();
}
catch(Exception e)
{
// return false;
}
}