当我点击启动它时,需要帮助找出为什么app崩溃和停止视频?

时间:2017-12-24 02:45:40

标签: java android android-mediarecorder

代码应该可以工作但是当我点击我的视频按钮停止录制时,我会收到一个"致命异常"崩溃,它告诉我mMediaRecorder.stop();行无法正常工作。

这是我的代码:

   private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback() {
    @Override
    public void onOpened(@NonNull CameraDevice camera) {  //camera device member return back to me

        mCameraDevice = camera;
       // Toast.makeText(getApplicationContext(), "Camera connection made!", Toast.LENGTH_SHORT).show();
        // ^indicates camera is connected

        if (mIsRecording){  

            try{
                createVideoFileName();
            }
            catch(IOException e) {
                e.printStackTrace();
            }

            startRecord();
            mMediaRecorder.start();
        }

        else {
            startPreview();
        }
    }







//makes this marshallow version compatible for Android because from Marshallow version and onwards we needed...
//...the permissions required.
private void checkWriteStoragePermission(){
    // v check to support applications on devices older than Marshmallow version in Android
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.lollipop) { //support Marshallow or later versions


        // v check to see if granted permission
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
                PackageManager.PERMISSION_GRANTED) {

            mIsRecording = true;
            mRecordImageButton.setImageResource(R.drawable.btn_video_busy);  //not recording

            try {
                createVideoFileName();  //was giving an unhandled exception java long error so needed try/catch here
            } catch (IOException e) {
                e.printStackTrace();
            }


            startRecord();
            mMediaRecorder.start();



        }/* else {        //check if permission was granted and had been denied
            if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                Toast.makeText(this, "app needs to be able to save videos", Toast.LENGTH_SHORT).show();
            }
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION_RESULT);
        } */
    }

    else //if running older versions than Marshallow, not going to worry about it, so build "else" statement
    {  // supporting old versions of Android
        mIsRecording = true;
        mRecordImageButton.setImageResource(R.drawable.btn_video_busy);  //not recording

        try{
            createVideoFileName();  
        }
        catch (IOException e){
            e.printStackTrace();
        }

        startRecord();
        mMediaRecorder.start();

    }
}







 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    createVideoFolder();  //creates the video folder when camera page loads

    mMediaRecorder = new MediaRecorder();

    mTextureView = (TextureView) findViewById(R.id.textureView);
    mRecordImageButton = (ImageButton) findViewById(R.id.videoOnlineImageButton);


    mRecordImageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mIsRecording) {

                mIsRecording = false; 
                mRecordImageButton.setImageResource(R.drawable.btn_video_online); 

                mMediaRecorder.stop();  //<-- FATAL EXCEPTION CRASH

                mMediaRecorder.reset(); 

                startPreview();  //< this code says = once it's stops recording TextureView preview still continues

            } else {
                checkWriteStoragePermission(); 

            }
        }
    });

}



    private void createVideoFolder() {   
    File movieFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
    mVideoFolder = new File(movieFile, "camera2VideoImage");     
    if(!mVideoFolder.exists()) { 
        mVideoFolder.mkdirs();
    } 
}





12-23 22:23:07.509 14045-14045/com.example.name.videoscrolltrial E/MediaRecorder: stop failed: -1007
12-23 22:23:07.510 14045-14045/com.example.name.videoscrolltrial D/AndroidRuntime: Shutting down VM
12-23 22:23:07.511 14045-14045/com.example.name.videoscrolltrial E/AndroidRuntime: FATAL EXCEPTION: main                                                          

Process: com.example.name.videoscrolltrial, PID: 14045
                                                                                        java.lang.RuntimeException: stop failed.
                                                                                            at android.media.MediaRecorder._stop(Native Method)
                                                                                            at android.media.MediaRecorder.stop(MediaRecorder.java:1220)
                                                                                            at com.example.name.videoscrolltrial.Camera$3.onClick(Camera.java:192) //< causes the crash, this is mMediaRecorder.stop();
                                                                                            at android.view.View.performClick(View.java:6308)
                                                                                            at android.view.View$PerformClick.run(View.java:23969)
                                                                                            at android.os.Handler.handleCallback(Handler.java:751)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                            at android.os.Looper.loop(Looper.java:154)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:6823)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)

此代码适用于Nexus 5X这样的手机,但对于我的三星Galaxy Note 8,它并不会造成崩溃。我不明白我必须做些什么来使代码兼容所有手机?

1 个答案:

答案 0 :(得分:0)

如果不首先启动mediaRecorder,则无法停止它。