gpt4 book ai didi

android - 当我停止录制时 MediaRecorder 失败

转载 作者:IT老高 更新时间:2023-10-28 21:54:48 26 4
gpt4 key购买 nike

我有这个错误。有人可以帮助我吗,我认为这是关于触摸监听器的事情......当我松开手指时发生错误。

04-25 20:07:00.263: D/FB Sessions(18429): false
04-25 20:07:04.533: I/MediaRecorderJNI(18429): prepare: surface=0x189250 (identity=1813)
04-25 20:07:10.493: E/MediaRecorder(18429): stop failed: -1007
04-25 20:07:10.493: D/AndroidRuntime(18429): Shutting down VM
04-25 20:07:10.493: W/dalvikvm(18429): threadid=1: thread exiting with uncaught exception (group=0x40018608)
04-25 20:07:10.503: E/AndroidRuntime(18429): FATAL EXCEPTION: main
04-25 20:07:10.503: E/AndroidRuntime(18429): java.lang.RuntimeException: stop failed.
04-25 20:07:10.503: E/AndroidRuntime(18429): at android.media.MediaRecorder.stop(Native Method)
04-25 20:07:10.503: E/AndroidRuntime(18429): at com.crewbase.rec.RecordActivity.stopRecording(RecordActivity.java:151)
04-25 20:07:10.503: E/AndroidRuntime(18429): at com.crewbase.rec.RecordActivity.access$2(RecordActivity.java:150)
04-25 20:07:10.503: E/AndroidRuntime(18429): at com.crewbase.rec.RecordActivity$1.onTouch(RecordActivity.java:79)
04-25 20:07:10.503: E/AndroidRuntime(18429): at android.view.View.dispatchTouchEvent(View.java:3897)
04-25 20:07:10.503: E/AndroidRuntime(18429): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-25 20:07:10.503: E/AndroidRuntime(18429): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-25 20:07:10.503: E/AndroidRuntime(18429): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-25 20:07:10.503: E/AndroidRuntime(18429): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
04-25 20:07:10.503: E/AndroidRuntime(18429): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1737)
04-25 20:07:10.503: E/AndroidRuntime(18429): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1153)
04-25 20:07:10.503: E/AndroidRuntime(18429): at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
04-25 20:07:10.503: E/AndroidRuntime(18429): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1721)
04-25 20:07:10.503: E/AndroidRuntime(18429): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2200)
04-25 20:07:10.503: E/AndroidRuntime(18429): at android.view.ViewRoot.handleMessage(ViewRoot.java:1884)
04-25 20:07:10.503: E/AndroidRuntime(18429): at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 20:07:10.503: E/AndroidRuntime(18429): at android.os.Looper.loop(Looper.java:130)
04-25 20:07:10.503: E/AndroidRuntime(18429): at android.app.ActivityThread.main(ActivityThread.java:3835)
04-25 20:07:10.503: E/AndroidRuntime(18429): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 20:07:10.503: E/AndroidRuntime(18429): at java.lang.reflect.Method.invoke(Method.java:507)
04-25 20:07:10.503: E/AndroidRuntime(18429): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-25 20:07:10.503: E/AndroidRuntime(18429): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-25 20:07:10.503: E/AndroidRuntime(18429): at dalvik.system.NativeStart.main(Native Method)

当我尝试运行这段代码时就会发生这种情况:

来自触摸监听器:

/// Preview is SurfaceView in my view
preview.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:

prepareRecording();
break;
case MotionEvent.ACTION_MOVE:
//Log.d(TAG, String.format("ACTION_MOVE | x:%s y:%s",
break;
case MotionEvent.ACTION_UP:
stopRecording();
break;
}
return true;
}
});

还有这两种方法:

private void prepareRecording() {
try {
camera.unlock();

recorder = new MediaRecorder();
recorder.setCamera(camera);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

File tempFile = new File(Environment.getExternalStorageDirectory(), "/rec/temp/video_" + String.valueOf(videoCount) + ".mp4");

recorder.setOutputFile(tempFile.getPath());
recorder.setVideoFrameRate(25);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
recorder.setPreviewDisplay(holder.getSurface());

recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
Log.e("REDORDING :: ",e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.e("REDORDING :: ",e.getMessage());
e.printStackTrace();
}
}

private void stopRecording() {
recorder.stop();
camera.lock();
}

最佳答案

看看documentation :

Note that a RuntimeException is intentionally thrown to the application, if no valid audio/video data has been received when stop() is called. This happens if stop() is called immediately after start(). The failure lets the application take action accordingly to clean up the output file (delete the output file, for instance), since the output file is not properly constructed when this happens.

换句话说:Dalvik 故意抛出异常。您必须处理它以在您的应用程序之后进行清理。你必须像这样处理它:

private void stopRecording() {
try {
recorder.stop();
} catch(RuntimeException stopException) {
// handle cleanup here
}
camera.lock();
}

关于android - 当我停止录制时 MediaRecorder 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16221866/

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