Anupal
Anupal

Reputation: 21

Displaying video using VideoView not working

While trying to play a video using VideoView, I am getting the error "Can't play this video" with an OK button. I tried two methods as shown in my codes, but the same error. Tried for a video on my Assets folder as well as in YouTube. Same error. I even checked the format for the video. It is in H.264 AVC format (MP4), which is a compatible format for MediaController.

Something is wrong in my codes. I am not able to find it. Please help. I am new to Android programming (just one month). Thanks in advance.

I have tried to find the answer in the past questions of this forum, the book 'Busy Coder's Guide for Android development' (current edition), and, developer.android.com/guides.

My code is as follows:

`enter code here' package com.example.anupal.mytestapp;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

import java.io.File;
import android.Manifest;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Environment;


public class VideoPlayback extends AppCompatActivity {
    private VideoView video;
    private MediaController mc;

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


     /*   File clip=new File("file:///android_asset/c10y16q3.mp4");
        video.setVideoPath(clip.getAbsolutePath()); */

        video = (VideoView)findViewById(R.id.videoView2);
        video.setVideoPath("file:///android_asset/bihu_dance.mp4");

        /* Method 2
        Uri uri = Uri.parse("file:///android_asset/bihu_dance.mp4");
        video.setVideoURI(uri);
        mc=new MediaController(this);
        mc.setMediaPlayer(video);
        video.setMediaController(mc);
        video.requestFocus(); */

        video.start();

    }
}

I just want the video to play, but am getting the error "Can't play this video" with an OK button.

Upvotes: 1

Views: 65

Answers (2)

Anupal
Anupal

Reputation: 21

I have found the mistake I was making. Answers posted in this forum helped me in finding the mistake.

There were two mistakes. (1) The raw folder (or any such folder) needs to be created from within Android Studio by right-clicking and choosing New. It should not be done from Windows OS, which I was doing.
(2) The video file should be placed in the raw folder by using 'copy and paste' method. It should not be 'dragged'; like I am (and many people are) accustomed to in Windows OS. It gets corrupted on 'dragging'.

So the mistake was not in the code, it was elsewhere.

Hope this answer will help others who may face similar problem. Thanks to all contributors whose answers helped me.

Upvotes: 1

Hesam Rasoulian
Hesam Rasoulian

Reputation: 1535

Create a folder with raw name and write your code like this:

VideoView videoView =(VideoView)findViewById(R.id.vdVw);
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.videoName);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();

if it doesn't work change your video and check it!

Upvotes: 0

Related Questions