Android Studio视频不显示或录制。

huangapple 未分类评论59阅读模式
标题翻译

Android Studio Video not displaying or recording

问题

我正在尝试使用 MediaRecorder 录制视频,并在另一个活动中显示该视频。起初一切都正常工作,视频也存在,但现在出现了一个错误,无法显示视频。有人可以帮忙吗?

这是我的代码:


captureButton.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        isActionDown = true;
        try {
            if (isActionDown) {

                initRecorder();

                if (isActionDown)

                    prepareRecorder();

                isPrepared = true;
            }
            if (isPrepared && isActionDown) {
                Toast.makeText(getContext(), "startRecorder", Toast.LENGTH_LONG).show();

                isRecording = true;
                mediaRecorder.start();


            } else {
                releaseMediaRecorder();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("onLongPress Error ", e.toString());
        }

        return true;

    }
});

captureButton.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {


        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:

                isActionDown = false;

                try {

                    if (isRecording) {

                        if (mediaRecorder != null) {
                            mediaRecorder.stop();
                            releaseMediaRecorder(); // release the MediaRecorder object
                            camera.lock();
                            Toast.makeText(getContext(), "MediaRecoderNull", Toast.LENGTH_LONG).show();
                        }
                        isRecording = false;
                        if (fileUri != null) {
                            playVideo(getView());
                        }
                    }
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
        return false;
    }
});


Uri fileUri = null;
Uri ImageUri;

private void initRecorder() {
    mediaRecorder = new MediaRecorder();

    camera.unlock();
    mediaRecorder.setCamera(camera);
    mediaRecorder.setOrientationHint(90);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

    mediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
}

private void prepareRecorder() {
    mediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
    try {
        mediaRecorder.prepare();
        isPrepared = true;
        return;
    } catch (IllegalStateException e) {
        releaseMediaRecorder();
        e.printStackTrace();

    } catch (IOException e) {
        releaseMediaRecorder();
        e.printStackTrace();

    }

}

public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;

private static Uri getOutputMediaFileUri(int type){
    return Uri.fromFile(getOutputMediaFile(type));
}

private static File getOutputMediaFile(int type){
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "MyCameraApp");
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_"+ timeStamp + ".jpg");
    } else if(type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "VID_"+ timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

public void playVideo(View view) {
    Intent playIntent = new Intent(getActivity(), CapturedVideoActivity.class);
    playIntent.putExtra("videoUri", fileUri.toString());
    startActivity(playIntent);
}

在显示下一个屏幕时发生了这种情况:

VideoView mVideoView = findViewById(R.id.videoCaptured);

videoUri = Uri.parse(getIntent().getExtras().getString("videoUri"));
mVideoView.setVideoURI(videoUri);
mVideoView.start();
英文翻译

I am trying to record video using mediarecorder and display that video on other activity. It was working fine and the video was there but now a error is occuring that video cannot be displayed. Can someone help?

Here my code :

     captureButton.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                isActionDown = true;
                try {
                    if (isActionDown) {

                        initRecorder();

                        if (isActionDown)

                            prepareRecorder();

                        isPrepared = true;
                    }
                    if (isPrepared && isActionDown) {
                        Toast.makeText(getContext(), "startRecorder", Toast.LENGTH_LONG).show();

                        isRecording = true;
                        mediaRecorder.start();


                    } else {
                        releaseMediaRecorder();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("onLongPress Error ", e.toString());
                }

                return true;

            }
        });

        captureButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {


                switch (event.getAction()) {
                    case MotionEvent.ACTION_UP:

                        isActionDown = false;

                        try {

                            if (isRecording) {

                                if (mediaRecorder != null) {
                                    mediaRecorder.stop();
                                    releaseMediaRecorder(); // release the MediaRecorder object
                                    camera.lock();
                                    Toast.makeText(getContext(), "MediaRecoderNull", Toast.LENGTH_LONG).show();
                                }
                                isRecording = false;
                                if (fileUri != null) {
                                    playVideo(getView());
                                }
                            }
                        } catch (IllegalStateException e) {
                            e.printStackTrace();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                }
                return false;
            }
        });
   

    Uri fileUri = null;
    Uri ImageUri;
private void initRecorder() {
        mediaRecorder = new MediaRecorder();

        camera.unlock();
        mediaRecorder.setCamera(camera);
        mediaRecorder.setOrientationHint(90);
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

        mediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
        fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
    }


    private void prepareRecorder() {
        mediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
        try {
            mediaRecorder.prepare();
            isPrepared = true;
            return;
        } catch (IllegalStateException e) {
            releaseMediaRecorder();
            e.printStackTrace();

        } catch (IOException e) {
            releaseMediaRecorder();
            e.printStackTrace();

        }

    }

    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;

    /** Create a file Uri for saving an image or video */
    private static Uri getOutputMediaFileUri(int type){
        return Uri.fromFile(getOutputMediaFile(type));
    }

    /** Create a File for saving an image or video */
    private static File getOutputMediaFile(int type){
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "MyCameraApp");
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE){
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".jpg");
        } else if(type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_"+ timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }


    public void playVideo(View view) {
        Intent playIntent = new Intent(getActivity(), CapturedVideoActivity.class);
        playIntent.putExtra("videoUri", fileUri.toString());
        startActivity(playIntent);
    }

so on the next screen while displaying this occurs enter image description here

Next screen code:

        VideoView mVideoView = findViewById(R.id.videoCaptured);

    videoUri = Uri.parse(getIntent().getExtras().getString("videoUri"));
    mVideoView.setVideoURI(videoUri);
    mVideoView.start();

huangapple
  • 本文由 发表于 2020年5月30日 19:44:27
  • 转载请务必保留本文链接:https://java.coder-hub.com/62101933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定