Muhammed Refaat
Muhammed Refaat

Reputation: 9103

Video from resources is not displaying

I'm trying to display an mp4 video from the app resources inside a VideoView, referring to this answer and this tutorial I used the following way:

// make the videoView visible
storyVideo.setVisibility(View.VISIBLE);
// set Video Preferences
storyVideo.requestFocus();
storyVideo.setBackgroundColor(Color.BLACK);
// get & set the video URI
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.story_media4);
storyVideo.setVideoURI(uri);
// Start The Video
storyVideo.start();

where story_media4 is stored in the raw file in resources:

enter image description here

Upvotes: 1

Views: 70

Answers (3)

Muhammed Refaat
Muhammed Refaat

Reputation: 9103

Actually the video was displaying but not showing, with the help of this answer, I could display the video after setting video_view.setZOrderOnTop(true); on both video URI setting and video prepared listener method:

// set Video Preferences
storyVideo.requestFocus();
storyVideo.setZOrderOnTop(true);
// get & set the video URI
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.story_media4);
storyVideo.setVideoURI(uri);
// when the video is ready for display
storyVideo.setOnPreparedListener(onVideoPrepared);

private MediaPlayer.OnPreparedListener onVideoPrepared = new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        storyVideo.setZOrderOnTop(true);
        // to start the video
        storyVideo.start();
    }
};

Update: For the above method, the video is going on Top of all of the views, if -for a reason- you want the video to be covered by one or more views (like sticker or description text) the best way is to set the background of the video to TRANSPARENT :

storyVideo.setBackgroundColor(Color.TRANSPARENT);

Upvotes: 1

vithika
vithika

Reputation: 233

public class Videoplay extends AppCompatActivity {

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


        VideoView vidView = (VideoView)findViewById(R.id.myVideo);

        MediaController vidControl = new MediaController(this);

        String vidAddress = "https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4";
        Uri vidUri = Uri.parse(vidAddress);

        Toast.makeText(Videoplay.this, "Loading Teaser",Toast.LENGTH_LONG).show();

        vidView.setVideoURI(vidUri);

        vidView.start();


        vidControl.setAnchorView(vidView);

        vidView.setMediaController(vidControl);


    }

Upvotes: 0

Amir Mohsenian
Amir Mohsenian

Reputation: 94

You should remove storyVideo.setBackgroundColor(Color.BLACK); . I test your code and remove setBackgroundColor(Color.BLACK) and fix this problem. Please test below code for OnPreparedListener() :

 videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            Log.i("LOG","Video test");
        }
    });

please remove videoView.start() in your onPrepared and test with Log().

Upvotes: 1

Related Questions