How do I add a video file (mp4) into my android application?

android, android-videoview, java, mp4

Solution

Very important the use of `setOnPreparedListener()` method, and be sure to have a video called `group_project.mp4` inside `/raw` folder.

  final VideoView videoview = (VideoView) findViewById(R.id.button10);
    Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.group_project);
    videoview.setVideoURI(uri);
    videoview.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                videoview.start();    
            }
        });

Read the documentation:

A MediaPlayer object must first enter the Prepared state before playback can be started. There are two ways (synchronous vs. asynchronous) that the Prepared state can be reached: either a call to prepare() (synchronous) which transfers the object to the Prepared state once the method call returns, or a call to prepareAsync() (asynchronous) which first transfers the object to the Preparing state after the call returns (which occurs almost right way) while the internal player engine continues working on the rest of preparation work until the preparation work completes. When the preparation completes or when prepare() call returns, the internal player engine then calls a user supplied callback method, onPrepared() of the OnPreparedListener interface, if an OnPreparedListener is registered beforehand via setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener).

Problem

I want to be able to add a video I created that automatically starts playing when it's brought up on the screen, so far my code looks like this: ``` VideoView videoview = (VideoView) findViewById(R.id.button10); Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.group_project); videoview.setVideoURI(uri); videoview.start(); ``` Currently nothing happens when I click and go on that screen. It plays sound when I used media player but I really want to add this video. any help would be much appreciated.

Original source