Search code examples
androidandroid-cameraandroid-videoviewplayback

Android Studio blank VideoView does not playback video recorded in emulator


I'm trying to record a video using the camera in the emulator. After recording, I would like the VideoView to display a preview image of the 1 millisecond into the clip. Then add a MediaController over the VideoView. The user has to click on a button to create intent to activate ACTION_VIDEO_CAPTURE. See UI layout here.

I can do the recording in the emulator's camera app, but the problem is I can't even get it to immediately playback in the VideoView. How can I achieve my aim in the 1st paragraph? I'm not getting any error messages from the device or in Logcat. After recording, it just goes back to the blank videoView. Could it be the video format?

FYI, I have an ImageView in the same screen which works perfectly fine.

onCreate method

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_new_card);

    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Create a new activity card");

    activity_title = findViewById(R.id.activity_title);
    imageView = findViewById(R.id.imageView);
    editText_hrs = findViewById(R.id.editText_hrs);
    editText_mins = findViewById(R.id.editText_mins);
    videoBtn = findViewById(R.id.uploadVideo);
    videoView = findViewById(R.id.videoView);
    checkbox = findViewById(R.id.check_useofitems);
    createcard_done = findViewById(R.id.createcard_done);

    drawer = findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    imageutils = new ImageUtils(this);
    imageView.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            imageutils.imagepicker(1);
        }
    });

    videoUtils = new VideoUtils(this);
    videoBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //videoUtils.videoPicker(1);
            //videoView.setVideoURI(videoUtils.getVideoUri());
            Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            videoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
            if(videoIntent.resolveActivity(getPackageManager()) != null){
                startActivityForResult(videoIntent, VIDEO_REQUEST);
            }
        }
    });
    videoView.setVideoURI(videoUri);
    videoView.start();

onActivityResult method

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch(requestCode) {
        case 0:
            imageutils.onActivityResult(requestCode, resultCode, data);
            break;
        case 1:
            if (resultCode == RESULT_OK) {
                videoUri = data.getData();
            }
            break;
    }
}

Solution

  • Ok, I managed to solve my problem, thank heavens. The problem lies with how I handled the requestCode cases in onActivityResult(). I have an ImageView and VideoView in the same activity. Both my ImageView and VideoView allows user to upload either from camera or gallery.

    This is the code to handle ImageView and VideoView in the onCreate() method:

    imageutils = new ImageUtils(this);
    imageView.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            imageutils.imagepicker(1);
        }
    });
    
    videoUtils = new VideoUtils(this);
    videoBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            videoUtils.videoPicker(1);
        }
    });
    

    In my ImageUtils class, I have a camera_call() and a gallery_call():

    public void camera_call() {
        ContentValues values = new ContentValues();
        imageUri = current_activity.getContentResolver().insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        current_activity.startActivityForResult(intent1, 0);
    }
    
    public void gallery_call() {
        Intent intent2 = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent2.setType("image/*");
        current_activity.startActivityForResult(intent2, 1);
    }
    

    And in my VideoUtils class, I also have a camera_call() and a gallery_call():

    public void camera_call() {
        Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        videoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
        current_activity.startActivityForResult(videoIntent, 2);
    }
    
    public void gallery_call() {
        Intent intent2 = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        intent2.setType("video/*");
        current_activity.startActivityForResult(intent2, 3);
    }
    

    As you know, the parameters for startActivityForResult is as such: startActivityForResult(intent, requestCode). So this is how I handle the different requestCodes for camera and gallery in ImageView and VideoView in the same activity, using onActivityResult():

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK) {
            if(requestCode == 0 || requestCode == 1) {
                imageutils.onActivityResult(requestCode, resultCode, data);
            }
            if(requestCode == 2 || requestCode == 3) {
                videoView.setVisibility(View.VISIBLE);
                videoUri = data.getData();
                videoView.setVideoURI(videoUri);
                videoView.setFocusable(true);
                videoView.seekTo(1);
                MediaController mc = new MediaController(this);
                videoView.setMediaController(mc);
                Log.i("True", "Executed");
            }
        }
    }
    

    That's it! Now I can upload and display an image in an activity, and upload and play a video in the same activity.