Media Recorder启动失败

时间:2018-09-05 09:49:06

标签: android google-glass

我想在Google Glass中制作视频录制应用程序。我尝试了一些方法,但是有一些错误。 应用程序启动时,将显示预览。然后我点击触摸板,开始录制。在mediaRecorder.start()代码上,出现了一些错误。

这是我的代码:

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;

import com.google.android.glass.touchpad.Gesture;
import com.google.android.glass.touchpad.GestureDetector;

import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

public class VideoActivity extends Activity implements SurfaceHolder.Callback {
    String fileOutput = "DCIM/Camera/imgtest.mp4";
    String path = Environment.getExternalStorageDirectory() + File.separator + fileOutput;
    private android.hardware.Camera mCamera;
    private MediaRecorder mediaRecorder;
    SurfaceHolder mHolder;
    private GestureDetector gestureDetector = null;
    private boolean isRecording = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preview);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        SurfaceView mPreview = (SurfaceView) findViewById(R.id.preview);
        mHolder = mPreview.getHolder();
        mHolder.addCallback(this);
        gestureDetector = createGestureDetector(this);
    }

    private GestureDetector createGestureDetector(Context context)
    {
        GestureDetector gestureDetector = new GestureDetector(context);
        gestureDetector.setBaseListener(new GestureDetector.BaseListener() {
            @Override
            public boolean onGesture(Gesture gesture) {
                if (gesture == Gesture.TAP)
                {
                    if (!isRecording) {
                        if (Recording()) {
                            mediaRecorder.start();
                            isRecording = true;
                        }
                        return true;
                    }
                    else {
                        mediaRecorder.stop();
                        mediaRecorder.reset();
                        mediaRecorder.release();
                        mediaRecorder = null;
                        isRecording = false;
                    }
                }
                return false;
            }
        });
        return gestureDetector;
    }

    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {
        if (gestureDetector != null) {
            return gestureDetector.onMotionEvent(event);
        }
        return false;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mCamera = Camera.open();
        this.setCameraParameters(mCamera);

        try {
            mCamera.setPreviewDisplay(holder);
        } catch (Exception e) {
            this.releaseCamera();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (mCamera != null) {
            mCamera.startPreview();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        this.releaseCamera();
    }

    public void releaseCamera() {
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
    }

    public void setCameraParameters(Camera camera) {
        if (camera != null) {
            Camera.Parameters parameters = camera.getParameters();
            parameters.setPreviewFpsRange(30000, 30000);
            camera.setParameters(parameters);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mCamera != null) {
            releaseCamera();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mCamera != null) {
            releaseCamera();
        }
    }

    private boolean Recording ()
    {
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        //mediaRecorder.setMaxDuration(1000);
        //mediaRecorder.setCaptureRate(15);
        //mediaRecorder.setVideoFrameRate(15);
        mediaRecorder.setOutputFile(path);
        mediaRecorder.setPreviewDisplay(mHolder.getSurface());
        try {
            mediaRecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
            mediaRecorder.release();
            return false;
        }
        return true;
    }
}

这是错误代码:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.glass.recordtest, PID: 9159
                  java.lang.RuntimeException: start failed.
                      at android.media.MediaRecorder.start(Native Method)
                      at com.example.glass.recordtest.VideoActivity$1.onGesture(VideoActivity.java:51)
                      at com.google.android.glass.touchpad.CombinedListener.onGesture(CombinedListener.java:101)
                      at com.google.android.glass.touchpad.OneFingerState.onEvent(OneFingerState.java:49)
                      at com.google.android.glass.touchpad.StateMachine.onEvent(StateMachine.java:79)
                      at com.google.android.glass.touchpad.GestureDetector.onMotionEvent(GestureDetector.java:244)
                      at com.example.glass.recordtest.VideoActivity.onGenericMotionEvent(VideoActivity.java:73)
                      at android.app.Activity.dispatchGenericMotionEvent(Activity.java:2502)
                      at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchGenericMotionEvent(PhoneWindow.java:2157)
                      at android.view.ViewRootImpl$ViewPostImeInputStage.processGenericMotionEvent(ViewRootImpl.java:3998)
                      at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3862)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3424)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3474)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3443)
                      at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3550)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3451)
                      at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3607)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3424)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3474)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3443)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3451)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3424)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3474)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3443)
                      at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3583)
                      at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3743)
                      at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2010)
                      at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1704)
                      at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1695)
                      at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1987)
                      at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
                      at android.os.MessageQueue.nativePollOnce(Native Method)
                      at android.os.MessageQueue.next(MessageQueue.java:138)
                      at android.os.Looper.loop(Looper.java:131)
                      at android.app.ActivityThread.main(ActivityThread.java:5061)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:515)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
                      at dalvik.system.NativeStart.main(Native Method)

我搜索了此错误并尝试了一些解决方案,但无法修复此错误。我不知道为什么会出现此错误。请帮助我解决此错误。

0 个答案:

没有答案