如何在我的Google Glass应用中录制视频?

时间:2018-08-27 01:16:11

标签: java android google-glass google-glass-camera

我尝试使用Android Studio录制程序。但是我收到了一些错误。

package com.example.glass.transfer1;

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

import android.annotation.SuppressLint;
import android.app.Activity;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

@SuppressLint("Registered")
public class VideoActivity extends Activity {
    String fileOutput = "DCIM/Camera/imgtest.mp4";
    String path = Environment.getExternalStorageDirectory() + fileOutput;
    private android.hardware.Camera mCamera;
    private MediaRecorder mediaRecorder;
    SurfaceHolder holder;

    /*@Override
    public void surfaceCreated(SurfaceHolder holder)
    {
        try
        {
            mCamera = Camera.open();
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

    @Override
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        if (mCamera != null)
        {
            mCamera.stopPreview();
            mCamera = null;
        }
    }*/

    SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try
            {
                mCamera = Camera.open();
                mCamera.setPreviewDisplay(holder);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

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

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            if (mCamera != null)
            {
                mCamera.stopPreview();
                mCamera = null;
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //mVideo = (VideoView) findViewById(R.id.preview);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        SurfaceView mPreview = (SurfaceView) findViewById(R.id.preview);
        setContentView(R.layout.preview);
        holder = mPreview.getHolder();
        holder.addCallback(surfaceCallback);
        Record(holder);
    }

    private void Record(SurfaceHolder holder)
    {
        if (mediaRecorder != null)
        {
            mediaRecorder.stop();
            mediaRecorder.release();
            mediaRecorder = null;
        }

        File videofile = new File(path);
        if (videofile.exists()) {
            videofile.delete();
        }
        if (mCamera != null)
        {
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
        try
        {
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            //mediaRecorder.setVideoSize(1280, 720);
            mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
            mediaRecorder.setMaxDuration(1000);
            mediaRecorder.setCaptureRate(15);
            mediaRecorder.setVideoFrameRate(15);
            mediaRecorder.setPreviewDisplay(holder.getSurface());
            mediaRecorder.setOutputFile(path);
            //mCamera.unlock();
            mediaRecorder.setCamera(mCamera);
            mediaRecorder.prepare();
            mediaRecorder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我收到错误:

  

E / Android运行时:致命异常:主要                    流程:com.example.glass.transfer1,PID:14146                    java.lang.RuntimeException:无法启动活动> ComponentInfo {com.example.glass.transfer1 / com.example.glass.transfer1.VideoActivity}:java.lang.NullPointerException                        在> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2235)                        在> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2285)                        在> android.app.ActivityThread.access $ 800(ActivityThread.java:138)                        在> android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1236)                        在android.os.Handler.dispatchMessage(Handler.java:102)                        在android.os.Looper.loop(Looper.java:149)                        在> android.app.ActivityThread.main(ActivityThread.java:5061)                        在java.lang.reflect.Method.invokeNative(本机方法)                        在java.lang.reflect.Method.invoke(Method.java:515)                        在> com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:794)                        在> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)                        在dalvik.system.NativeStart.main(本机方法)                     造成原因:java.lang.NullPointerException                        在> com.example.glass.transfer1.VideoActivity.onCreate(VideoActivity.java:94)                        在> android.app.Activity.performCreate(Activity.java:5236)                        在> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1089)                        在> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)                        在> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2285)                        在> android.app.ActivityThread.access $ 800(ActivityThread.java:138)                        在> android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1236)                        在android.os.Handler.dispatchMessage(Handler.java:102)                        在android.os.Looper.loop(Looper.java:149)                        在> android.app.ActivityThread.main(ActivityThread.java:5061)                        在java.lang.reflect.Method.invokeNative(本机方法)                        在java.lang.reflect.Method.invoke(Method.java:515)                        在> com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:794)                        在> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)                        在dalvik.system.NativeStart.main(本机方法)

如何解决此错误?谢谢。

1 个答案:

答案 0 :(得分:1)

您收到java.lang.NullPointerException异常是因为您在findViewById之前调用setContentView

   @Override
    public void onCreate(Bundle savedInstanceState) {
        /..

        SurfaceView mPreview = (SurfaceView) findViewById(R.id.preview);
        setContentView(R.layout.preview);

        /..
    }

setContentView方法,如documentation所说:

  

将活动内容设置为显式视图。该视图已放置   直接进入活动的视图层次。

因此,您尝试查找ID为SurfaceView的{​​{1}}特定视图,而未将该视图设置为活动。

您应该尝试在它们之间切换

preview