代码之家  ›  专栏  ›  技术社区  ›  Subin Babu

拍摄视频时camera.startCapturing视频(null);自动调用公共void onVideoTaken(文件视频)

  •  1
  • Subin Babu  · 技术社区  · 7 年前

    https://github.com/natario1/CameraView

    当我拍视频的时候,有时它对我来说很好。但有时在拍视频的时候camera.startCapturingVideo(空);自动调用公共void onVideoTaken(文件视频)。

    摄像头活动代码:

    Oncreate():

     setContentView(R.layout.activity_home);
            ButterKnife.bind(this);
            Fabric.with(this, new Crashlytics());
            logUser();
            view_group_photo.performClick();
            if (BuildConfig.DEBUG) {
                CameraLogger.setLogLevel(CameraLogger.LEVEL_VERBOSE);
            }
            recorder = new MediaRecorder();
            camera = findViewById(R.id.camera);
            camera.destroy();
            camera.start();
            camera.addCameraListener(new CameraListener() {
                @Override
                public void onCameraError(@NonNull CameraException exception) {
                    super.onCameraError(exception);
                    camera.stop();
                    camera.start();
                }
    
                @Override
                public void onPictureTaken(byte[] jpeg) {
                    super.onPictureTaken(jpeg);
                    Long tsLong = System.currentTimeMillis() / 1000;
                    String ts = tsLong.toString();
                    try {
                        File root = new File(FILE_PATH_IMAGE);
                        if (!root.exists()) {
                            root.mkdirs();
                        }
                        @SuppressLint("SdCardPath") File myFile = new File(FILE_PATH_IMAGE + ts + FILE_EXTENSION_IMAGE);
                        myFile.createNewFile();
                        FileOutputStream stream = new FileOutputStream(myFile);
                        stream.write(jpeg);
                        stream.close();
                        Intent webViewIntent = new Intent(HomeActivity.this, ImageCropActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("file_path", FILE_PATH_IMAGE + ts + FILE_EXTENSION_IMAGE);
                        webViewIntent.putExtras(bundle);
                        webViewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(webViewIntent);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                @Override
                public void onVideoTaken(File video) {
                    super.onVideoTaken(video);
                    Long tsLong = System.currentTimeMillis() / 1000;
                    String ts = tsLong.toString();
                    try {
                        File root = new File(FILE_PATH_VIDEO);
                        if (!root.exists()) {
                            root.mkdirs();
                        }
                        @SuppressLint("SdCardPath") File myFile = new File(FILE_PATH_VIDEO + ts + FILE_EXTENSION_VIDEO);
                        myFile.createNewFile();
                        try (InputStream in = new FileInputStream(video)) {
                            try (OutputStream out = new FileOutputStream(myFile)) {
                                // Transfer bytes from in to out
                                byte[] buf = new byte[1024];
                                int len;
                                while ((len = in.read(buf)) > 0) {
                                    out.write(buf, 0, len);
                                }
                                out.close();
                            }
                        }
                        AddFileToPojo("2", FILE_TYPE_VIDEO, FILE_PATH_VIDEO + ts + FILE_EXTENSION_VIDEO);
                        IntentFunction(HomeActivity.this, ReportIncidentActivity.class);
                    } catch (IOException e) {
                        Log.e("error ", "video error" + e.getMessage());
                    }
                }
            });
    
    
            mGoogleApiClient = new GoogleApiClient
                    .Builder(this)
                    .enableAutoManage(this, 0, HomeActivity.this)
                    .addApi(Places.GEO_DATA_API)
                    .addApi(Places.PLACE_DETECTION_API)
                    .addConnectionCallbacks(HomeActivity.this)
                    .addOnConnectionFailedListener(this)
                    .build();
    
            guessCurrentPlace();
            // get the gesture detector
            mDetector = new GestureDetector(this, new MyGestureListener());
            if (camera.getFlash() == Flash.ON) {
                imageViewFlash.setImageDrawable(getResources().getDrawable(R.drawable.flash));
            } else {
                imageViewFlash.setImageDrawable(getResources().getDrawable(R.drawable.flash_off));
            }
    
            viewGroupProgressLoad.setOnTouchListener(touchListener);
    
            ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            assert connectivityManager != null;
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected()) {
                ConstantVariables.INTERNET_CONNECTION = true;
                if (DEBUG) {
                    Log.i("", ":: " + ConstantVariables.INTERNET_CONNECTION);
                }
            } else {
                ConstantFunctions.IntentFunction(getApplicationContext(), NoInternetActivity.class);
                ConstantVariables.INTERNET_CONNECTION = false;
                if (DEBUG) {
                    Log.i("", ":: " + ConstantVariables.INTERNET_CONNECTION);
                }
            }
    

     if (camera.getSessionType() == SessionType.PICTURE) {
                camera.setSessionType(SessionType.VIDEO);
            }
            if (camera.getSessionType() != SessionType.VIDEO) {
                return;
            }
            if (mCapturingPicture || mCapturingVideo) return;
            mCapturingVideo = true;
            /*camera.setVideoMaxSize(1000000);*/
            camera.setVideoMaxDuration(300000);
            camera.startCapturingVideo(null);
    

    摄像机视频停止fn();

     camera.stopCapturingVideo();
     camera.destroy();
     myCountDownTimer.cancel();
     isSpeakButtonLongPressed = false;
    

    日志:

    I/CameraController: Restart: returned from start. Dispatching. State: STATE_STARTED
    I/CameraCallbacks: dispatchOnCameraOpened com.otaliastudios.cameraview.CameraOptions@61755df
    W/MediaRecorder: mediarecorder went away with unhandled events
    W/MediaRecorder: mediarecorder went away with unhandled events
    I/CameraController: Stop: executing. State: STATE_STARTED
    Stop: about to call onStop()
    I/Camera1: onStop: About to clean up.
    onStop: Clean up. Ending video.
    endVideoImmediately: is capturing: false
    onStop: Clean up. Stopping preview.
    I/Camera1: onStop: Clean up. Stopped preview.
    onStop: Clean up. Releasing camera.
    I/Camera1: onStop: Clean up. Released camera.
    W/Camera1: onStop: Clean up. Returning.
    I/CameraController: Stop: returned from onStop(). Dispatching.
    I/CameraCallbacks: dispatchOnCameraClosed
    E/MediaMetadataRetrieverJNI: getFrameAtTime: videoFrame is a NULL pointer
    I/Choreographer: Skipped 67 frames! The application may be doing too much work on its main thread.
    I/CameraPreview: crop: applied scaleX= 1.1407409
    crop: applied scaleY= 1.0
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Subin Babu    6 年前

    我更新了代码,

    final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    camera.startCapturingVideo(null);
                }
            }, 1000);
    

    startCapturingVideo 初始化并开始记录。因为硬件在某些情况下响应不快。所以下一行的执行由于 开始播放视频 没有受到威胁。

    推荐文章